0

我正在尝试解析一些 JSON,但遇到了一些麻烦。

这是我已经尝试过的:

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);
            }
        }
    } 

我只是想从 中获取类别,JSONObject但由于某种原因jsonString从未评估为children.

这是结构,抱歉,我粘贴了这么多部分,我发现 JSON 真的很混乱,很难在保持其完整性的同时尝试将其削减,请记住我只是想获得该name字段:

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

6 回答 6

12

您需要使用 String.equals() 来比较字符串:

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

jsonString == "children" 比较对象引用,而不是字符串。

于 2012-09-23T16:38:15.010 回答
3

children是一个包含 JSON 数组的 JSON 对象。

您可以通过以下方式调用从 JSON 对象中提取子对象:

    JSONObject json=new JSONObject(improvedInputStreamAsString);
    JSONObject json1=json.getJSONObject("children");
于 2012-10-10T13:35:30.907 回答
3

我会为此推荐两个库,一个已经在上面提到过,但为了清楚起见。

杰克逊

http://jackson.codehaus.org/

GSON

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

于 2012-10-10T15:27:57.120 回答
2

我觉得问题是

keys.next()不会按预期返回对象,而是返回迭代器的下一个键。因此尝试以下

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);
        }
    }
}

由于 JSON 字符串似乎在重复一种格式,因此最好编写一个递归函数来完成您的任务。

于 2012-09-23T17:29:32.733 回答
2

它更好地Jackson用于解析JSON

请参阅此链接:

http://jackson.codehaus.org/

于 2012-09-23T16:53:24.337 回答
0

首先使您的 Json 有效

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\"                }            ]        }    ]}";

现在在您的 json 中,您在 JSONArray 中有 JSONArray,它位于另一个 JSONArray 中。所以你必须使用 3 for 循环作为

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();
    }

它将为您提供所需的名称字段。只需尝试此代码。

于 2012-10-10T15:23:09.220 回答