Parse JSON from IBM Watson Personality Insights and get values in Android -
i having json
output not able parse after lot of try. new this. getting json
string ibm watson personality insights. output following, can me how can parse ? need name,id,percentage , sampling error of json
objects in it. how can achieve in 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 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 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 change", "name": "openness 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 } ] } ] } ] } }
jsonobject object = new jsonobject(yourjsonstring); arraylist<map<string,object>> datalist= new arraylist<map<string,object>>(); { datalist = getdata(object.getjsonobject("tree").getjsonarray("children"), datalist,object.getjsonobject("tree")); }while (object.getjsonobject("tree").getjsonarray("children").length()>0);
and here function:
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; }
at end datalist
arraylist
contain , objects have
if (object.has("name") && object.has("id") && object.has("percentage") && object.has("sampling_error"))
Comments
Post a Comment