Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Converting JSON to Java Object Array

avatar
Contributor

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.

1 ACCEPTED SOLUTION

avatar
Master Mentor
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login
6 REPLIES 6

avatar
Master Mentor
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login

avatar
Contributor

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

avatar
Master Mentor

@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}

avatar
Contributor

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.

avatar
Master Mentor

absolutely @Dayou Zhou please accept it as best answer if it helped. Thanks

avatar

@Dayou Zhou

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"