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の大部分を貼り付けたため、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

これには2つのライブラリをお勧めします。1つはすでに上で説明しましたが、わかりやすくするためです。

ジャクソン

http://jackson.codehaus.org/

GSON

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

于 2012-10-10T15:27:57.120 に答える
3

childrenは、JSON配列を含むJSONオブジェクトです。

次の方法で呼び出すことにより、JSONオブジェクトから子抽出できます。

    JSONObject json=new JSONObject(improvedInputStreamAsString);
    JSONObject json1=json.getJSONObject("children");
于 2012-10-10T13:35:30.907 に答える
2

Jackson構文解析に使用する方がよいでしょうJSON

このリンクを参照してください:

http://jackson.codehaus.org/

于 2012-09-23T16:53:24.337 に答える
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 に答える
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内にあります。したがって、3forループを次のように使用する必要があります。

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 に答える