Created 01-31-2017 01:06 AM
I have a JSON string (of type object) as follows:
{"field1":1,"field2":"abc"}
and I would like to convert it to a Java Object[2], where the 1st element is new Integer(1) and the 2nd element is new String("abc"). I tried both Jackson and GSON but couldn't find a way to do this conversion. Any help would be appreciated, thank you.
Created 01-31-2017 01:12 AM
Perhaps you should paste your code for suggestion but this website is one I always reference http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Created 01-31-2017 01:12 AM
Perhaps you should paste your code for suggestion but this website is one I always reference http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Created 01-31-2017 01:24 AM
Hi @Artem Ervits, yes I checked that too, but I need an Object array which seems to be the only case not covered...
Jackson:
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, Object[].class);
Error: Can not deserialize instance of java.lang.Object[] out of START_OBJECT token at [Source: {"f1":1,"f2":"abc"}; line: 1, column: 1]
GSON:
Gson gson = new Gson();
return gson.fromJson(json, Object[].class);
Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
Created 01-31-2017 02:19 AM
@Dayou Zhou using json-simple
import org.json.JSONObject; public class JSONParser { public static void main(String[] args) { String jsonStr = "{\"field1\":1,\"field2\":\"abc\"}"; JSONObject json = new JSONObject(jsonStr); Person person = new Person(); person.setKey(json.getInt("field1")); person.setValue(json.getString("field2")); System.out.println(person.toString()); } }
class Person { int key; String value; @Override public String toString() { return "Person{" + "key=" + key + ", value=" + value + '}'; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
maven dependency
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> <type>jar</type> </dependency>
output
Person{key=1, value=abc}
Created 02-01-2017 01:56 AM
Hi @Artem Ervits, yes looks like this is what I need, and is perhaps the only way for my use case. Thank you for your help.
Created 02-01-2017 01:58 AM
absolutely @Dayou Zhou please accept it as best answer if it helped. Thanks
Created 01-31-2017 01:52 AM
Here is the correct example to parse your JSON.
import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class JSONParser { public static void main(String[] args) throws IOException { try(Reader reader = new InputStreamReader(JsonToJava.class.getResourceAsStream("Server1.json"), "UTF-8")){ Gson gson = new GsonBuilder().create(); Person p = gson.fromJson(reader, Person.class); System.out.println(p); } } }
Person.java as
public class Person { private String field1; private String field2; @Override public String toString() { return field1 + " - " + field2; } }
Command to run: java -cp .:gson-2.2.2.jar JSONParser
output should be "1 - abc"