0

I am making an app in which i am getting a string as response from server side. that response is encoded in json. Now my problem is How to do json parsing of the encoded response from server side to decode it.. I am geting the following response from server

["[email protected]","[email protected]","facebook_514728069"]

It is in the form of an array.I want to decde it and display as textviws in my another activity. I am using the following code:

 HttpClient client = new DefaultHttpClient();
 String getURL = "http://www.ondamove.it/English/mob/profile_friends.php?email="+Login.eMailId+"";
 HttpGet get = new HttpGet(getURL);
 HttpResponse responseGet = client.execute(get); 
 HttpEntity resEntityGet = responseGet.getEntity(); 
 if (resEntityGet != null) 
 { 
 String s = EntityUtils.toString(resEntityGet);
 System.out.println(s);
 JSONArray ids = new JSONArray(s);
 for(int i=0; i< ids.length(); i++){
 System.out.println(ids[]); //print each of the string in json array.
 }

but it is giving me the error : The type of the expression must be an array type but it resolved to JSONArray

how to resolve this issue. can anyone help me over this? thanks

asked Oct 7, 2011 at 6:39

3 Answers 3

1

You can use JSONTokener to parse JSON documents for example:

String json = "{"
 + " \"query\": \"Pizza\", "
 + " \"locations\": [ 94043, 90210 ] "
 + "}";
 JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
 String query = object.getString("query");
 JSONArray locations = object.getJSONArray("locations");
answered Oct 7, 2011 at 6:46
Sign up to request clarification or add additional context in comments.

2 Comments

How can i do this i am getting respone from server side and i waNT TO PARSE THAT
You have to get the server response as a String object "String jsonResponseString = Sever.getResponse();" then you can parse as described above.
1

get your response as string

String jsonResponseString = Sever.getResponse();
JSONArray ids = new JSONArray(jsonResponseString);
for(int i=0; i< ids.length(); i++){
 Log.i(TAG, ids[i]); //print each of the string in json array.
}
answered Oct 7, 2011 at 6:44

7 Comments

ur code is giving folowing error : The type of the expression must be an array type but it resolved to JSONArray
the response you are getting is not json. it should start with a '{' . can you please check this out.
can you post the value that you are getting in String "s". i would like to see if it is json.
["[email protected]","[email protected]","facebook_514728069"] is the value that i am getting from server side
then it is not json. either send a valid json from the server, or use string functions to manipulate the string output.
|
0

GSON if you just want to keep it simple. Jackson streaming if you need raw speed.

Or you could always use the built in JSON tools -- but I'd recommend one of these other two.

answered Oct 7, 2011 at 6:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.