Parsing JSON?

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Parsing JSON?

Postby momob » Tue Aug 04, 2009 8:38 pm

Hello guys,

I did it! I started Android dev and it is a lot of fun. For my first app, I am trying to upload an image to a server which return a JSON string/object in this form:

serverResponse = {"category_outputs": [0.099017499999999994, 0.096076599999999998, 0.098027299999999998, 0.097047099999999997, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], "imageid": "2009-07-23-17.22.32.287593.png"}

I am only interested in the float/numbers and would like to convert this server return string into a float array that looks like:

array = [0.099017499999999994, 0.096076599999999998, 0.098027299999999998, 0.097047099999999997, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

My understanding is that Android has it own JSON parsing method but I am not sure how to use them. I see something like:

org.json, JSONArray, JSONObject..and so on but I have no clue how to actually use them. I have looked at the Google info but they do not have an example on how to use that parser other than explaining the object themselves.

I am basically stuck with my little project of uploading an image to a server. The uploading and return works great but I need to figure out the parsing part.

Thanks for any pointers.


Mo.

ps: By the way, plusminus you did a heck of job with this site! I have learned so much looking at your tutorials it is amazing. This seems to be a vibrant community and I am extremely I joined. THANKS!
momob
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jul 13, 2009 8:55 pm

Top

Postby desuvinodkumar » Wed Aug 05, 2009 6:39 am

HI Momob

Is these also JSON Response or ANy other ,.....

getRatingResponse{return=ContestInfo{item=anyType{name=Ankitha; totalimages=2; rating=2.5; }; item=anyType{name=Anushka; totalimages=4; rating=9.5; }; item=anyType{name=Apsara; totalimages=1; rating=0; };

how can i parse it give me the solution if u know how to get data from the response and show in the list

Thanks a lot in advance
Vinod...
User avatar
desuvinodkumar
Senior Developer
Senior Developer
 
Posts: 102
Joined: Mon Mar 23, 2009 6:51 am
Location: India

Postby momob » Wed Aug 05, 2009 7:14 am

Hello Vinod.

I will assume the string you show seems to be a JSON object. Unfortunately that's exactly what I am trying to find out myself. In my case the string is much more simpler than yours but I will assume that any JSON parser will work on your problem as well.


Sorry for not being too helpful!

Mo.
momob
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jul 13, 2009 8:55 pm

Postby padde » Thu Aug 06, 2009 1:10 am

padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby momob » Thu Aug 06, 2009 7:23 am

Thanks so much padde! That's a cool find (the second link...i was aware of the first among many others) Let me try it and I will report here my experience.

Thanks again. I appreciate you take the time.

Mo,
momob
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jul 13, 2009 8:55 pm

Postby momob » Mon Aug 10, 2009 4:21 am

Hello guys,

I wanted to thank you again for your help and suggestions. I think I finally figure out how to parse my JSON return. I wanted to share the code I found:

//----------------------------------------------------------------

try{
JSONObject json = new JSONObject(serverResponse);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);

}

catch (JSONException e) {
Log.e("JSON", "There was an error parsing the JSON", e);
}

///---------------------------------------------------------------------------

If I apply to the JSON object above in my post, I get the following for (serverText is a texView window)


serverText.setText(valArray.getString(0));



[0.099017499999999994, 0.096076599999999998, 0.098027299999999998, 0.097047099999999997, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Which is what I wanted.


Thanks and enjoy!

Mo.

ps: I do not understand much yet the code above so I may not be much help to answer about it! All I know is that display a JSONarray from the server return...
momob
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jul 13, 2009 8:55 pm

Top

connect to server

Postby mobile developer » Fri Nov 06, 2009 12:39 pm

Hi momob thanks for sharing the code

I have some problems regarding to get the response from the server.
I have to post the data to server according to that the response comes in JSON.
Problems:
1. How can I connect to server?
2. How to post the data to server?
3. How to set the response in JSONObject so that I can parse it?
I have tried different codes but not get success.
Can u provide me some snippets

Thanks in Advance.
mobile developer
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 05, 2009 9:41 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 7 guests