Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Converting JSON to Java Object Array

Solved Go to solution

Converting JSON to Java Object Array

Explorer

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

Accepted Solutions

Re: Converting JSON to Java Object Array

Mentor

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/

6 REPLIES 6

Re: Converting JSON to Java Object Array

Mentor

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/

Re: Converting JSON to Java Object Array

Explorer

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

Re: Converting JSON to Java Object Array

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}

Re: Converting JSON to Java Object Array

Explorer

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.

Re: Converting JSON to Java Object Array

Mentor

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

Re: Converting JSON to Java Object Array

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

Don't have an account?
Coming from Hortonworks? Activate your account here