0

I am currently developing for android and using some face recognition services. The service returns some complex JSON as seen below;

EDIT - Proper JSON

{"photos":[{"url":"http:\/\/face.com\/images\/ph\/0b3ecef9a00b647570027e590b72c893.jpg","pid":"F@ddc704534bc71ad7271a1d07b95362ca_88f17d8482a3d3aac1d07b3062a3e29b","width":960,"height":637,"tags":[{"tid":"TEMP_F@ddc704534bc71ad7271a1d07b95362ca_88f17d8482a3d3aac1d07b3062a3e29b_54.06_45.53_1_1","recognizable":true,"threshold":59,"uids":[{"uid":"1@greene99","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}}

The only thing I need to get out of it is the uid value inside the uids array. Have tried a few methods with no success, any advice?

EDIT - answer :)

String uid = new JSONObject(inputString).getJSONArray("photos").getJSONObject(0).getJSONArray("tags").getJSONObject(0).getJSONArray("uids").getJSONObject(0).getString("uid");
5
  • Those '-' chars aren't there right or are they? Commented Mar 28, 2012 at 13:32
  • You are right sorry, seems to make it easier to read they added them into there formatted return preview, they are not actually passed through. Commented Mar 28, 2012 at 13:35
  • post what you have tried, and what jsonparser you're using Commented Mar 28, 2012 at 13:35
  • Post the normal json, we can watch the json using jsonformat.com or some other json formatter. Easier than this to read... Commented Mar 28, 2012 at 13:42
  • Edited topic to the normal json Commented Mar 28, 2012 at 13:51

3 Answers 3

5

Try this example using the json.org library ( 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");

Where input string is the string representation of your JSON.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply, I tried this but it throws JSONException and dosnt work.
Now that you have posted proper JSON, I updated my code. Try it again!
Then read it and post it or try to correct the error on your own. To read the first item in a JSON Array "array" : [{item}] use getJSONArray("array").getJSONObject(0) and to get a value from a JSONObject use getX() whre X can be String, Int, ...
Your code does work, when I C&P'ed it it didnt work however when I built it up from scratch it works. Odd but thanks :)
0

Checkout JSONTokener

http://developer.android.com/reference/org/json/JSONTokener.html

1 Comment

Thanks Shubhayu, I have parsed Json before just none that has been this complex as it seems to be an array inside an array inside an array. Any more relevant to the problem help would be appreciated :)
0

Hey this is not a proper JSON. use jsonlint http://jsonlint.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.