Parsing JSON Object in Java

I have JSON object as follows:

member = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";

In Java I want to parse the above jsonobject and store the values in an arraylist.

Can any one please provide me some code snippet through which i can achieve this.

Thanks

Parsing Json to Java Object

got this problem: Json: {authenticationToken:{token:c9XXXX1-XXXX-4XX9-XXXX-41XXXXX3XXXX}} Object: public class AuthenticationToken { public AuthenticationToken() { } public AuthenticationToken

Parsing JSON array to java object

I am trying to parse using Jackson mapper to parse big JSON to java object. I have very big JSON but came across this little piece in it and not sure how to parse. Here is the JSON, the format of it l

parsing JSON in java?

How to parsing JSON in java? { code: 100, message: SUCCESS, result: { list: [ { cardNumber: ALUFZZ5SZ1Q5, expireTime: 1270742400, surplusThreshold: 4, uid: 771292, useTime: 12702

parsing json with java

I am a newbie to json parsing, I have grabbed a json string from a request and now I need to parse it with java. I’m using json-lib for this. But I’m really stuck as I’m not familiar with it. I need t

Json string parsing to java object with multiple objects

Trying to parse the following json string to java object using gson { entry: 132456, product: { item: 123456, prompts: [ { promptId: 1, promptNumber: 109, promptType: 4, promptT

Parsing a JSON array to a Java object

I have the following JSON returned by a REST API call. Is it even possible to read this construct into a Java object? here is the JSON data: [ { dirName: /, files: [ { fileName: Dog prototype

Parsing Json response to a different Java object each time

I am using Jersey as a client to parse JSON into Java objects, The problem is that I am using a service that returns different types of responses that should be mapped to a different Java object each

Error parsing json to java object: expecting number, got string

I am calling a service which returns responses as json and turn them into a Java object using Gson. In 47 of 50 cases, I get the Java object however in 3 of the cases, I get the following error: com.

Json object contain json array parsing

Am using a function to add list items,name ,id into a jsonobject and returning that object into called function,and getting all the way..But i am little confused with parsing of that json string. JSON

Parsing JSON string to .NET Object

IĀ“m parsing a JSON string to a corresponding .NET Object with the Newtonsoft library. I have a problem parsing JSON properties that are arrays. Sometimes the JSON property is an array, other times, it

Answers

In the past I have used gson, here you have the project home:

http://code.google.com/p/google-gson/

And here the user guide:

http://sites.google.com/site/gson/gson-user-guide

I’m assuming you want to store the interestKeys in a list.

Using the org.json library:

JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");

List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){
    list.add(array.getJSONObject(i).getString("interestKey"));
}

I think Jackson is the one you need to do with JSON.

public class JsonParsing {

public static Properties properties = null;

public static JSONObject jsonObject = null;

static {
    properties = new Properties();
}

public static void main(String[] args) {

    try {

        JSONParser jsonParser = new JSONParser();

        File file = new File("src/main/java/read.json");

        Object object = jsonParser.parse(new FileReader(file));

        jsonObject = (JSONObject) object;

        parseJson(jsonObject);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public static void getArray(Object object2) throws ParseException {

    JSONArray jsonArr = (JSONArray) object2;

    for (int k = 0; k < jsonArr.size(); k++) {

        if (jsonArr.get(k) instanceof JSONObject) {
            parseJson((JSONObject) jsonArr.get(k));
        } else {
            System.out.println(jsonArr.get(k));
        }

    }
}

public static void parseJson(JSONObject jsonObject) throws ParseException {

    Set<Object> set = jsonObject.keySet();
    Iterator<Object> iterator = set.iterator();
    while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (jsonObject.get(obj) instanceof JSONArray) {
            System.out.println(obj.toString());
            getArray(jsonObject.get(obj));
        } else {
            if (jsonObject.get(obj) instanceof JSONObject) {
                parseJson((JSONObject) jsonObject.get(obj));
            } else {
                System.out.println(obj.toString() + "/t"
                        + jsonObject.get(obj));
            }
        }
    }
}}

Thank you soooooooo much to CODE I can read any JSON file thanks to your code šŸ™‚ Now, I’m trying to organize all the elements by levels, for could use them!

I was working with Android reading a JSON from an URL and the only I had to change was the lines

Set<Object> set = jsonObject.keySet(); Iterator<Object> iterator = set.iterator();

for

Iterator<?> iterator = jsonObject.keys();

I share my implementation, to help someone:

public void parseJson(JSONObject jsonObject) throws ParseException, JSONException {

    Iterator<?> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        String obj = iterator.next().toString();

        if (jsonObject.get(obj) instanceof JSONArray) {
            //Toast.makeText(MainActivity.this, "Objeto: JSONArray", Toast.LENGTH_SHORT).show();
            //System.out.println(obj.toString());
            TextView txtView = new TextView(this);
            txtView.setText(obj.toString());
            layoutIzq.addView(txtView);
            getArray(jsonObject.get(obj));
        } else {
            if (jsonObject.get(obj) instanceof JSONObject) {
                //Toast.makeText(MainActivity.this, "Objeto: JSONObject", Toast.LENGTH_SHORT).show();
                parseJson((JSONObject) jsonObject.get(obj));
            } else {
                //Toast.makeText(MainActivity.this, "Objeto: Value", Toast.LENGTH_SHORT).show();
                //System.out.println(obj.toString() + "/t"+ jsonObject.get(obj));
                TextView txtView = new TextView(this);
                txtView.setText(obj.toString() + "/t"+ jsonObject.get(obj));
                layoutIzq.addView(txtView);
            }
        }
    }
}