“Java Deserialize JSON Object” Kode Jawaban

Java Deserialize JSON Array

@Test
public void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect() {
    String json = "[{"intValue":1,"stringValue":"one"}," +
      "{"intValue":2,"stringValue":"two"}]";
    Foo[] targetArray = new GsonBuilder().create().fromJson(json, Foo[].class);

    assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(1, "one")));
    assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(2, "two")));
    assertThat(Lists.newArrayList(targetArray), not(hasItem(new Foo(1, "two"))));
}
electriclance

Java Deserialize JSON Object

@Test
public void whenDeserializingToSimpleObject_thenCorrect() {
    String json = "{"intValue":1,"stringValue":"one"}";

    Foo targetObject = new Gson().fromJson(json, Foo.class);

    assertEquals(targetObject.intValue, 1);
    assertEquals(targetObject.stringValue, "one");
}
electriclance

Jawaban yang mirip dengan “Java Deserialize JSON Object”

Pertanyaan yang mirip dengan “Java Deserialize JSON Object”

Lebih banyak jawaban terkait untuk “Java Deserialize JSON Object” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya