0

我有一个JSON输出,即使经过大量尝试,我也无法解析。我是新来的。我JSON从 IBM Watson Personality Insights 得到字符串。输出如下,任何人都可以帮助我如何解析这个?JSON我需要其中所有对象的名称、ID、百分比和采样误差。我怎样才能在 Android 中实现这一点?有什么帮助吗?

{
"id": "*UNKNOWN*",
"source": "*UNKNOWN*",
"word_count": 2196,
"tree": {
    "id": "r",
    "name": "root",
    "children": [
      {
          "id": "personality",
          "name": "Big 5 ",
          "children": [
            {
                "id": "Openness_parent",
                "name": "Openness",
                "category": "personality",
                "percentage": 0.9493716242287923,
                "children": [
                  {
                      "id": "Openness",
                      "name": "Openness",
                      "category": "personality",
                      "percentage": 0.9493716242287923,
                      "sampling_error": 0.14430105599999998,
                      "children": [
                        {
                            "id": "Adventurousness",
                            "name": "Adventurousness",
                            "category": "personality",
                            "percentage": 0.7224550516937974,
                            "sampling_error": 0.11646272
                        },
                        {
                            "id": "Artistic interests",
                            "name": "Artistic interests",
                            "category": "personality",
                            "percentage": 0.16797400413558944,
                            "sampling_error": 0.22150304
                        },
                        {
                            "id": "Emotionality",
                            "name": "Emotionality",
                            "category": "personality",
                            "percentage": 0.3511636831395896,
                            "sampling_error": 0.12653872
                        },
                        {
                            "id": "Imagination",
                            "name": "Imagination",
                            "category": "personality",
                            "percentage": 0.9359684240034555,
                            "sampling_error": 0.159424064
                        },
                        {
                            "id": "Intellect",
                            "name": "Intellect",
                            "category": "personality",
                            "percentage": 0.9385421354297238,
                            "sampling_error": 0.139541408
                        },
                        {
                            "id": "Liberalism",
                            "name": "Authority-challenging",
                            "category": "personality",
                            "percentage": 0.9397610870913766,
                            "sampling_error": 0.181580384
                        }
                      ]
                  },
                  {
                      "id": "Conscientiousness",
                      "name": "Conscientiousness",
                      "category": "personality",
                      "percentage": 0.4704661610570499,
                      "sampling_error": 0.16413872000000002,
                      "children": [
                        {
                            "id": "Achievement striving",
                            "name": "Achievement striving",
                            "category": "personality",
                            "percentage": 0.41042556748073755,
                            "sampling_error": 0.150096064
                        },
                        {
                            "id": "Cautiousness",
                            "name": "Cautiousness",
                            "category": "personality",
                            "percentage": 0.8830744858589791,
                            "sampling_error": 0.176573024
                        },
                        {
                            "id": "Dutifulness",
                            "name": "Dutifulness",
                            "category": "personality",
                            "percentage": 0.18520605114000702,
                            "sampling_error": 0.22338736
                        },
                        {
                            "id": "Orderliness",
                            "name": "Orderliness",
                            "category": "personality",
                            "percentage": 0.2650491742919862,
                            "sampling_error": 0.144884736
                        },
                        {
                            "id": "Self-discipline",
                            "name": "Self-discipline",
                            "category": "personality",
                            "percentage": 0.11612292004852835,
                            "sampling_error": 0.180976736
                        },
                        {
                            "id": "Self-efficacy",
                            "name": "Self-efficacy",
                            "category": "personality",
                            "percentage": 0.8555219248412528,
                            "sampling_error": 0.19383872
                        }
                      ]
                  },
                  {
                      "id": "Extraversion",
                      "name": "Extraversion",
                      "category": "personality",
                      "percentage": 0.21040698860972917,
                      "sampling_error": 0.195489728,
                      "children": [
                        {
                            "id": "Activity level",
                            "name": "Activity level",
                            "category": "personality",
                            "percentage": 0.024541756361588174,
                            "sampling_error": 0.233987424
                        },
                        {
                            "id": "Assertiveness",
                            "name": "Assertiveness",
                            "category": "personality",
                            "percentage": 0.2427216753807347,
                            "sampling_error": 0.223443392
                        },
                        {
                            "id": "Cheerfulness",
                            "name": "Cheerfulness",
                            "category": "personality",
                            "percentage": 0.09384227477826083,
                            "sampling_error": 0.174551392
                        },
                        {
                            "id": "Excitement-seeking",
                            "name": "Excitement-seeking",
                            "category": "personality",
                            "percentage": 0.026173771491396755,
                            "sampling_error": 0.174486368
                        },
                        {
                            "id": "Friendliness",
                            "name": "Outgoing",
                            "category": "personality",
                            "percentage": 0.07591889688715117,
                            "sampling_error": 0.18887705600000002
                        },
                        {
                            "id": "Gregariousness",
                            "name": "Gregariousness",
                            "category": "personality",
                            "percentage": 0.012370191256998266,
                            "sampling_error": 0.207567712
                        }
                      ]
                  },
                  {
                      "id": "Agreeableness",
                      "name": "Agreeableness",
                      "category": "personality",
                      "percentage": 0.22070048778859377,
                      "sampling_error": 0.184531712,
                      "children": [
                        {
                            "id": "Altruism",
                            "name": "Altruism",
                            "category": "personality",
                            "percentage": 0.24291109323474566,
                            "sampling_error": 0.220757376
                        },
                        {
                            "id": "Cooperation",
                            "name": "Cooperation",
                            "category": "personality",
                            "percentage": 0.8173934882004398,
                            "sampling_error": 0.206217728
                        },
                        {
                            "id": "Modesty",
                            "name": "Modesty",
                            "category": "personality",
                            "percentage": 0.02818209310957665,
                            "sampling_error": 0.20777542400000001
                        },
                        {
                            "id": "Morality",
                            "name": "Uncompromising",
                            "category": "personality",
                            "percentage": 0.2260706216428393,
                            "sampling_error": 0.190937056
                        },
                        {
                            "id": "Sympathy",
                            "name": "Sympathy",
                            "category": "personality",
                            "percentage": 1,
                            "sampling_error": 0.223389728
                        },
                        {
                            "id": "Trust",
                            "name": "Trust",
                            "category": "personality",
                            "percentage": 0.21376942069250085,
                            "sampling_error": 0.214015392
                        }
                      ]
                  },
                  {
                      "id": "Neuroticism",
                      "name": "Emotional range",
                      "category": "personality",
                      "percentage": 0.4270412211997681,
                      "sampling_error": 0.12296604800000001,
                      "children": [
                        {
                            "id": "Anger",
                            "name": "Fiery",
                            "category": "personality",
                            "percentage": 0.4610136124167775,
                            "sampling_error": 0.117587392
                        },
                        {
                            "id": "Anxiety",
                            "name": "Prone to worry",
                            "category": "personality",
                            "percentage": 0.15360379639459695,
                            "sampling_error": 0.133953056
                        },
                        {
                            "id": "Depression",
                            "name": "Melancholy",
                            "category": "personality",
                            "percentage": 0.305584938548785,
                            "sampling_error": 0.165140704
                        },
                        {
                            "id": "Immoderation",
                            "name": "Immoderation",
                            "category": "personality",
                            "percentage": 0.17594715587324708,
                            "sampling_error": 0.118422368
                        },
                        {
                            "id": "Self-consciousness",
                            "name": "Self-consciousness",
                            "category": "personality",
                            "percentage": 0.39346884050797637,
                            "sampling_error": 0.184906112
                        },
                        {
                            "id": "Vulnerability",
                            "name": "Susceptible to stress",
                            "category": "personality",
                            "percentage": 0.26281736696691754,
                            "sampling_error": 0.136928736
                        }
                      ]
                  }
                ]
            }
          ]
      },
      {
          "id": "needs",
          "name": "Needs",
          "children": [
            {
                "id": "Practicality_parent",
                "name": "Practicality",
                "category": "needs",
                "percentage": 0.9839864449532627,
                "children": [
                  {
                      "id": "Challenge",
                      "name": "Challenge",
                      "category": "needs",
                      "percentage": 0.6584166017026339,
                      "sampling_error": 0.602517824
                  },
                  {
                      "id": "Closeness",
                      "name": "Closeness",
                      "category": "needs",
                      "percentage": 0.924529448337046,
                      "sampling_error": 0.696857696
                  },
                  {
                      "id": "Curiosity",
                      "name": "Curiosity",
                      "category": "needs",
                      "percentage": 0.836074789385127,
                      "sampling_error": 0.6318680640000001
                  },
                  {
                      "id": "Excitement",
                      "name": "Excitement",
                      "category": "needs",
                      "percentage": 0.8576434327454714,
                      "sampling_error": 0.62010672
                  },
                  {
                      "id": "Harmony",
                      "name": "Harmony",
                      "category": "needs",
                      "percentage": 0.981853123726956,
                      "sampling_error": 0.686543104
                  },
                  {
                      "id": "Ideal",
                      "name": "Ideal",
                      "category": "needs",
                      "percentage": 0.3425618908848262,
                      "sampling_error": 0.603128032
                  },
                  {
                      "id": "Liberty",
                      "name": "Liberty",
                      "category": "needs",
                      "percentage": 0.3936046680718908,
                      "sampling_error": 0.572532448
                  },
                  {
                      "id": "Love",
                      "name": "Love",
                      "category": "needs",
                      "percentage": 0.5237755189252377,
                      "sampling_error": 0.721777024
                  },
                  {
                      "id": "Practicality",
                      "name": "Practicality",
                      "category": "needs",
                      "percentage": 0.9839864449532627,
                      "sampling_error": 0.65719744
                  },
                  {
                      "id": "Self-expression",
                      "name": "Self-expression",
                      "category": "needs",
                      "percentage": 0.03828102978032242,
                      "sampling_error": 0.64546576
                  },
                  {
                      "id": "Stability",
                      "name": "Stability",
                      "category": "needs",
                      "percentage": 0.7025224350455281,
                      "sampling_error": 0.67650368
                  },
                  {
                      "id": "Structure",
                      "name": "Structure",
                      "category": "needs",
                      "percentage": 0.6938192628712627,
                      "sampling_error": 0.026310336
                  }
                ]
            }
          ]
      },
      {
          "id": "values",
          "name": "Values",
          "children": [
            {
                "id": "Conservation_parent",
                "name": "Conservation",
                "category": "values",
                "percentage": 0.020928720964750132,
                "children": [
                  {
                      "id": "Conservation",
                      "name": "Conservation",
                      "category": "values",
                      "percentage": 0.020928720964750132,
                      "sampling_error": 0.251261408
                  },
                  {
                      "id": "Openness to change",
                      "name": "Openness to change",
                      "category": "values",
                      "percentage": 0.9738559906182246,
                      "sampling_error": 0.26176976
                  },
                  {
                      "id": "Hedonism",
                      "name": "Hedonism",
                      "category": "values",
                      "percentage": 0.36249280957482843,
                      "sampling_error": 0.256016768
                  },
                  {
                      "id": "Self-enhancement",
                      "name": "Self-enhancement",
                      "category": "values",
                      "percentage": 0.4220518949469879,
                      "sampling_error": 0.24324576
                  },
                  {
                      "id": "Self-transcendence",
                      "name": "Self-transcendence",
                      "category": "values",
                      "percentage": 0.8232322720898714,
                      "sampling_error": 0.233368096
                  }
                ]
            }
          ]
      }
    ]
  }
}
4

1 回答 1

2
JSONObject object = new JSONObject(yourjsonstring);
ArrayList<Map<String,Object>> dataList= new ArrayList<Map<String,Object>>();
do {
      dataList = getData(object.getJSONObject("tree").getJSONArray("children"), dataList,object.getJSONObject("tree"));
}while (object.getJSONObject("tree").getJSONArray("children").length()>0);

这里的功能:

public ArrayList<Map<String,Object>> getData(JSONArray ja,ArrayList<Map<String,Object>> container,JSONObject previousObj){
    try {
        for(int i=0;i<ja.length();i++) {
            JSONObject object = ja.getJSONObject(i);
            if (object.has("name") && object.has("id") && object.has("percentage") && object.has("sampling_error")) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", object.get("name"));
                map.put("id", object.get("id"));
                map.put("percentage", object.get("percentage"));
                map.put("sampling_error", object.get("sampling_error"));
                if (!container.contains(map))
                    container.add(map);
            }
            if (object.has("children"))
                if (object.getJSONArray("children").length() > 0)
                    return getData(object.getJSONArray("children"), container, object);
        }
    previousObj.put("children",new JSONArray());
    }catch (JSONException e){}
    return container;
}

最后dataList ArrayList将包含所有且仅包含具有

if (object.has("name") && object.has("id") && object.has("percentage") && object.has("sampling_error")) 
于 2015-06-14T00:55:08.990 回答