2012-03-28 77 views
0

我目前正在爲android開發並使用一些人臉識別服務。該服務返回一些複雜的JSON,如下所示;Android中複雜的JSON解析 - 多對象和數組

編輯 - 正確的JSON

{"photos":[{"url":"http:\/\/face.com\/images\/ph\/0b3ecef9a00b647570027e590b72c893.jpg","pid":"[email protected]_88f17d8482a3d3aac1d07b3062a3e29b","width":960,"height":637,"tags":[{"tid":"[email protected]_88f17d8482a3d3aac1d07b3062a3e29b_54.06_45.53_1_1","recognizable":true,"threshold":59,"uids":[{"uid":"[email protected]","confidence":17}],"gid":null,"label":"","confirmed":false,"manual":false,"tagger_id":null,"width":19.58,"height":29.51,"center":{"x":54.06,"y":45.53},"eye_left":{"x":49.55,"y":38.8},"eye_right":{"x":58.4,"y":38.92},"mouth_left":{"x":49.82,"y":52.1},"mouth_center":{"x":54.77,"y":53.04},"mouth_right":{"x":58.36,"y":52.16},"nose":{"x":54.98,"y":46.3},"ear_left":null,"ear_right":null,"chin":null,"yaw":-7.59,"roll":2.27,"pitch":0.01,"attributes":{"age_est":{"value":18,"confidence":30},"age_max":{"value":24,"confidence":30},"age_min":{"value":12,"confidence":30},"face":{"value":"true","confidence":99},"gender":{"value":"female","confidence":41},"glasses":{"value":"false","confidence":73},"lips":{"value":"parted","confidence":63},"mood":{"value":"happy","confidence":61},"smiling":{"value":"true","confidence":48}}}]}],"status":"success","usage":{"used":1,"remaining":4999,"limit":5000,"reset_time_text":"Wed, 28 Mar 2012 14:20:08 +0000","reset_time":1332944408}} 

我需要擺脫它的唯一的事情是UID的數組中的UID值。嘗試過幾種方法沒有成功,有什麼建議?

編輯 - 答案:)

String uid = new JSONObject(inputString).getJSONArray("photos").getJSONObject(0).getJSONArray("tags").getJSONObject(0).getJSONArray("uids").getJSONObject(0).getString("uid"); 
+0

那些' - '字符不對,或者它們是? – Ferdau 2012-03-28 13:32:32

+0

對不起,似乎更容易閱讀,他們將它們添加到格式化的返回預覽中,但它們實際上並未通過。 – user1298268 2012-03-28 13:35:23

+0

發佈您嘗試過的內容,以及您使用的是什麼jsonparser – 2012-03-28 13:35:34

回答

5

使用json.org庫(http://www.json.org/java/index.html)試試這個例子:

String uid = new JSONObject(inputString).getJSONArray("photos").getJSONObject(0).getJSONArray("tags").getJSONObject(0).getJSONArray("uids").getJSONObject(0).getString("uid"); 

其中輸入字符串爲您的JSON字符串表示。

+0

感謝您的回覆,我試過這個,但它引發了JSONException和dosnt的工作。 – user1298268 2012-03-28 13:49:29

+0

現在你已經發布了適當的JSON,我更新了我的代碼。再試一次! – damaxxed 2012-03-28 13:58:45

+0

仍然拋出異常:( – user1298268 2012-03-28 14:05:30