0

i have two situation of Json output . one is data that found and i have a json array and a json object like this:

{"data":"yes"}[{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]

other situation is that data not found :

{"data":"no"}

just one json object.

how parse this data in android client for support two situtaion?

2
  • 1
    The "yes" version is not valid json, so there won't be any problem parsing it... Commented Dec 2, 2014 at 19:44
  • how i can implement that mode? use two array? Commented Dec 2, 2014 at 19:51

2 Answers 2

1

First, you should validate your json in http://jsonlint.com/ if you test it you will look that is a wrong json. So, for make it right, in your server your response should look something like this:

{"data":"yes","response":[{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]}

And in that case, in android

JSONObject jsonObj = new JSONObject(response); 
if (jsonObj.getString("data").compareTo("yes") == 0) {
   JSONArray jsonArray = jsonObj.getJSONArray("response");
   //To-Do another code
}

and that's all

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

Comments

1

Here is a possible case: (you need to fix your json format)

Success -

 string resultJSON = 
   {"success":true,
      "data":[
          {"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},
          {"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]}

Failed -

string resultJSON =
    {"success":false}

Then

JSONObject jsonRoot  = new JSONObject(resultJSON);
 bool isSuccess = jsonRoot.getBoolean("success");
 if (isSuccess) {
  // do the array parser
  for(int i=0; i<jsonData.lenght;i++) {
        JSONObject jsonObj = jsonData.getJSONObject(i);
        String id = jsonObj.getString("id");   // get the value of id
        String desc = jsonObj.getString("desc");  // and so on...
  }
 }

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.