0

I'm trying to parse some JSON but I'm having some trouble.

Here's what I have already tried:

ContentValues categories = new ContentValues();     
JSONObject json = new JSONObject(improvedInputStreamAsString);
Iterator keys = json.keys();

while(keys.hasNext()) {
        Object newObj = keys.next();
        String jsonString = newObj.toString();
        if(jsonString == "children") {
            JSONObject newJsn = new JSONObject(jsonString);
            if(newJsn.has("name")) {
                String category = newJsn.getString("name");
                categories.put("categories", category);
            }
        }
    } 

I'm only trying to get the category from the JSONObject but for some reason jsonString never evaluates to children.

Here's the structure, sorry I pasted such a large portion I find JSON really confusing and it was hard to try and cut it down while retaining its integrity, please remember I am only trying to get the name field:

{
  "position": "0",
  "children": [
{
  "position": "1",
  "children": [
    {
      "position": "1",
      "children": [
        {
          "position": "3",
          "children": [

          ],
          "category_id": "9",
          "level": "3",
          "is_active": "1",
          "name": "Exfoliants",
          "parent_id": "4"
        },
        {
          "position": "4",
          "children": [

          ],
          "category_id": "10",
          "level": "3",
          "is_active": "1",
          "name": "Moisturisers",
          "parent_id": "4"
        },
        {
          "position": "5",
          "children": [

          ],
          "category_id": "11",
          "level": "3",
          "is_active": "1",
          "name": "Treatments",
          "parent_id": "4"
        },
        {
          "position": "6",
          "children": [

          ],
          "category_id": "12",
          "level": "3",
          "is_active": "1",
          "name": "Cleansers",
          "parent_id": "4"
        },
        {
          "position": "7",
          "children": [

          ],
          "category_id": "13",
          "level": "3",
          "is_active": "1",
          "name": "Skin Toners",
          "parent_id": "4"
        },
        {
          "position": "9",
          "children": [

          ],
          "category_id": "15",
          "level": "3",
          "is_active": "1",
          "name": "Serums",
          "parent_id": "4"
        },
        {
          "position": "10",
          "children": [

          ],
          "category_id": "36",
          "level": "3",
          "is_active": "1",
          "name": "Acne and Blemish Treatments",
          "parent_id": "4"
        },
        {
          "position": "11",
          "children": [

          ],
          "category_id": "40",
          "level": "3",
          "is_active": "1",
          "name": "Oils",
          "parent_id": "4"
        }
      ],
      "category_id": "4",
      "level": "2",
      "is_active": "1",
      "name": "Skincare",
      "parent_id": "2"
    },
    {
      "position": "2",
      "children": [
        {
          "position": "1",
          "children": [

          ],
          "category_id": "16",
          "level": "3",
          "is_active": "1",
          "name": "Sunscreen",
          "parent_id": "5"
        },
        {
          "position": "2",
          "children": [

          ],
          "category_id": "31",
          "level": "3",
          "is_active": "1",
          "name": "Insect Repellent & After Bite",
          "parent_id": "5"
        },
        {
          "position": "3",
          "children": [

          ],
          "category_id": "32",
          "level": "3",
          "is_active": "1",
          "name": "After Sun Lotions",
          "parent_id": "5"
        },
        {
          "position": "4",
          "children": [

          ],
          "category_id": "33",
          "level": "3",
          "is_active": "1",
          "name": "Self Tanning Lotions",
          "parent_id": "5"
        }
      ],
      "category_id": "5",
      "level": "2",
      "is_active": "1",
      "name": "Suncare",
      "parent_id": "2"
    },
    {
      "position": "3",
      "children": [
        {
          "position": "1",
          "children": [

          ],
          "category_id": "17",
          "level": "3",
          "is_active": "1",
          "name": "Shampoo",
          "parent_id": "6"
        },
        {
          "position": "2",
          "children": [

          ],
          "category_id": "18",
          "level": "3",
          "is_active": "1",
          "name": "Conditioner",
          "parent_id": "6"
        },
        {
          "position": "3",
          "children": [

          ],
          "category_id": "19",
          "level": "3",
          "is_active": "1",
          "name": "Treatments",
          "parent_id": "6"
        },
        {
          "position": "4",
          "children": [

          ],
          "category_id": "20",
          "level": "3",
          "is_active": "1",
          "name": "Toner",
          "parent_id": "6"
        },
        {
          "position": "6",
          "children": [

          ],
          "category_id": "34",
          "level": "3",
          "is_active": "1",
          "name": "Styling",
          "parent_id": "6"
        },
        {
          "position": "7",
          "children": [

          ],
          "category_id": "39",
          "level": "3",
          "is_active": "1",
          "name": "Serum",
          "parent_id": "6"
        }
      ],
      "category_id": "6",
      "level": "2",
      "is_active": "1",
      "name": "Hair",
      "parent_id": "2"
    },



{
  "position": "0",
  "children": [
    {
      "position": "1",
      "children": [
        {
      "position": "1",
      "children": [
        {
          "position": "3",
          "children": [                
          ],
          "category_id": "9",
          "level": "3",
          "is_active": "1",
          "name": "Exfoliants",
          "parent_id": "4"
        },            
        {
          "position": "6",
          "children": [

          ],
          "category_id": "12",
          "level": "3",
          "is_active": "1",
          "name": "Cleansers",
          "parent_id": "4"
        },
     ],
      "category_id": "4",
      "level": "2",
      "is_active": "1",
      "name": "Skincare",
      "parent_id": "2"
    },
    {
      "position": "2",
      "children": [
        {
          "position": "1",
          "children": [

          ],
          "category_id": "16",
          "level": "3",
          "is_active": "1",
          "name": "Sunscreen",
          "parent_id": "5"
        },
4
  • 1
    Just by looking at the end of that massive JSON fragment I can tell there are a load of unmatched [s. Commented Sep 23, 2012 at 15:51
  • Could that be just because I have only given half of it, would the rest contain the closing brackets or do you think the JSON is corrupt somehow? Commented Sep 23, 2012 at 16:02
  • 1
    @SamJackson Yeah paste all the json for us. OR run your json through a syntax check like jsonlint.com or jsonformatter.curiousconcept.com Commented Oct 9, 2012 at 21:28
  • Your json has 22 errors..first fix your response Commented Oct 10, 2012 at 15:42

6 Answers 6

12

You need to use String.equals() to compare strings:

if(jsonString.equals("children")) { ... }

jsonString == "children" compares the object references, not the strings.

Sign up to request clarification or add additional context in comments.

Comments

3

children is a JSON object containing a JSON array.

You can extract children from a JSON object by calling in this fashion:

    JSONObject json=new JSONObject(improvedInputStreamAsString);
    JSONObject json1=json.getJSONObject("children");

Comments

3

I would recommend two libraries for this, one has already been mentioned above, but for clarity.

Jackson

http://jackson.codehaus.org/

GSON

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

Comments

2

Its better you use Jackson for parsing JSON.

See this link:

http://jackson.codehaus.org/

Comments

2
+50

I thing the problem is

keys.next() wont return the object as you expected, instead return the next key of the iterator. Therefore try kind of a following

while(keys.hasNext()) {
    String key = keys.next();
    if(key.equals("children")) {
        //this might throw exception, surround with try catch block
        JSONObject newJsn = new JSONObject(json.get(key));
        if(newJsn.has("name")) {
            String category = newJsn.getString("name");
            categories.put("categories", category);
        }
    }
}

Since the JSON string seems to be repeating a single format, better to write a recursive function to get your task done.

Comments

0

First of all make your Json valid as

String str="{   \"position\": \"0\",    \"children\": [        {            \"position\": \"1\",            \"children\": [                {                    \"position\": \"1\",                    \"children\": [                        {                            \"position\": \"3\",                            \"children\": [],                            \"category_id\": \"9\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Exfoliants\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"4\",                            \"children\": [],                            \"category_id\": \"10\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Moisturisers\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"5\",                            \"children\": [],                            \"category_id\": \"11\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Treatments\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"6\",                            \"children\": [],                            \"category_id\": \"12\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Cleansers\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"7\",                            \"children\": [],                            \"category_id\": \"13\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Skin Toners\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"9\",                            \"children\": [],                            \"category_id\": \"15\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Serums\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"10\",                            \"children\": [],                            \"category_id\": \"36\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Acne and Blemish Treatments\",                            \"parent_id\": \"4\"                        },                        {                            \"position\": \"11\",                            \"children\": [],                            \"category_id\": \"40\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Oils\",                            \"parent_id\": \"4\"                        }                    ],                    \"category_id\": \"4\",                    \"level\": \"2\",                    \"is_active\": \"1\",                    \"name\": \"Skincare\",                    \"parent_id\": \"2\"                },                {                    \"position\": \"2\",                    \"children\": [                        {                            \"position\": \"1\",                            \"children\": [],                            \"category_id\": \"16\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Sunscreen\",                            \"parent_id\": \"5\"                        },                        {                            \"position\": \"2\",                            \"children\": [],                            \"category_id\": \"31\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Insect Repellent & After Bite\",                            \"parent_id\": \"5\"                        },                        {                            \"position\": \"3\",                            \"children\": [],                            \"category_id\": \"32\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"After Sun Lotions\",                            \"parent_id\": \"5\"                        },                        {                            \"position\": \"4\",                            \"children\": [],                            \"category_id\": \"33\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Self Tanning Lotions\",                            \"parent_id\": \"5\"                        }                    ],                    \"category_id\": \"5\",                    \"level\": \"2\",                    \"is_active\": \"1\",                    \"name\": \"Suncare\",                    \"parent_id\": \"2\"                },                {                    \"position\": \"3\",                    \"children\": [                        {                            \"position\": \"1\",                            \"children\": [],                            \"category_id\": \"17\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Shampoo\",                            \"parent_id\": \"6\"                        },                        {                            \"position\": \"2\",                            \"children\": [],                            \"category_id\": \"18\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Conditioner\",                            \"parent_id\": \"6\"                        },                        {                            \"position\": \"3\",                            \"children\": [],                            \"category_id\": \"19\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Treatments\",                            \"parent_id\": \"6\"                        },                        {                            \"position\": \"4\",                            \"children\": [],                            \"category_id\": \"20\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Toner\",                            \"parent_id\": \"6\"                        },                        {                            \"position\": \"6\",                            \"children\": [],                            \"category_id\": \"34\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Styling\",                            \"parent_id\": \"6\"                        },                        {                            \"position\": \"7\",                            \"children\": [],                            \"category_id\": \"39\",                            \"level\": \"3\",                            \"is_active\": \"1\",                            \"name\": \"Serum\",                            \"parent_id\": \"6\"                        }                    ],                    \"category_id\": \"6\",                    \"level\": \"2\",                    \"is_active\": \"1\",                    \"name\": \"Hair\",                    \"parent_id\": \"2\"                }            ]        }    ]}";

Now in your json you have JSONArray inside JSONArray and which is inside another JSONArray. So you have to use 3 for loop as

try 
    {

        JSONObject job1=new JSONObject(str);
        JSONArray jarr1=job1.getJSONArray("children");


        for(int i=0;i<jarr1.length();i++)
        {

            JSONObject job2=new JSONObject(jarr1.getString(i));
            JSONArray jarr2=job2.getJSONArray("children");

            for(int j=0;j<jarr2.length();j++)
            {
                JSONObject job3=new JSONObject(jarr2.getString(j));
                System.out.println("job3........"+job3);

                JSONArray jarr3=job3.getJSONArray("children");
                for(int k=0;k<jarr3.length();k++)
                {
                    JSONObject job4=new JSONObject(jarr3.getString(k));
                    System.out.println("name.........."+job4.getString("name"));
                }

            }

        }



    } 
    catch (Exception e) {
        System.out.println("ex..................."+e);
        e.printStackTrace();
    }

It will give you required name field.Just try this code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.