2

I have 2 links; which gives json data. I am trying to get the values from the urls from the same activity in android using asynTask. did the coding till converting the data to string(stored it in jsonStr1). But now comes the problem. Because,among the 2 urls: one starts with JSON object-

{    "contacts": [     {"id": "c200", "name": "Ravi Tamada"  },

                       { "id": "c201", "name": "Johnny Depp" }
                ]
 }

another start with JSON array-

 [{"appeId":"1","survId":"1"}, 
   {"appeId":"2","survId":"32"}
  ]

Now how am i going to give a condition for them whether to know its a JSON array or Object? JSON array are object that i know but cant find how to separate them. i have tried the below:

JSONObject jsonObj = new JSONObject(jsonStr1);
   if(jsonObj instanceof JSONArray){}

but if condition is showing error- incompatible conditional operand types JSONObject and JSONArray

1
  • You have to write two different JSON parser for each URL. You will already what are you expecting either Object or Array. Commented Feb 18, 2014 at 7:40

4 Answers 4

5

You can simply use startsWith for String to check where the String starts with { or [

boolean isJsonArray = jsonResponse.startsWith("[");
if(isJsonArray){
   // Its a json array
}
else{
   // Its a json object
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSONTokener class for that, here is a sample code for that.

Object json = new JSONTokener(response).nextValue();
if (json instanceof JSONObject){
  JSONObject result = new JSONObject(response);
}
else if (json instanceof JSONArray){
    JSONArray resultArray = new JSONArray(response);
}

Comments

0

make jsonobject and call which place u want to calling

                    jsonobject = JSONfunctions
                    .getJSONfromURL("http://ampndesigntest.com/androidapi/CURRENTPROJECTDATA/textfiles/hotels");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("worldpopulation");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("rank", jsonobject.getString("rank"));
                map.put("country", jsonobject.getString("country"));
                map.put("population", jsonobject.getString("population"));
                map.put("flag", jsonobject.getString("flag"));
            //    map.put("latlongitude", jsonobject.getString("latlongitude"));


                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;

Comments

0

You could use the method has(String) to check if the JSONObject contains or not the key contacts.

if(jsonOjbect.has("contacts") {
   ...
}
else {
   ...
}

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.