1

im trying to get data from the site which have mongodb as their database and CI ,i make simple script to make json encode output from the site and the output is like :

   {
   "mko680": {
       "_id": {
       "$id": "515be1807bfb8b1d0d000000"
        },
       "channel": [
                  "channel a",
                  "subchannel a"
                  ],
       "channel_id": 227,
       "id": "mko680",
    }, 
   "mkv002": {
       "_id": {
       "$id": "515b32407bfb8b1d0d000000"
             },
       "channel": [
                 "channel a",
                 "subchannel b"
                   ],
       "channel_id": 232,
       "id": "mkv002",
  }
}

i try to parse that output in my android project like this

JSONArray obj = new JSONArray(outputlike o);
for (int i = 0; i < obj.length(); i++) {
  JSONObject json_data = obj.getJSONObject(i);
  Log.i("test",json_data.getString("channel_id"));
}

the logcat said org.JSON.Mismatch

any clue for which is json/my code that not right ?

thanks , and sory for my bad question hope you understand

UPDATED :

now i change it to json object like :

JSONObject arr = new JSONObject(bufstring);
for (int i = 0; i < arr.length(); i++) {
Log.i("test",arr.getString("channel_id"));
}

but the logcat now said , no value for channel_id, but it sure there is channel_id in that output, any clue ?

3 Answers 3

2

The data you receive is of the type JSONObject and not JSONArray. Therefore, you need to parse your json data like this:-

String jsonStr = "..."; //Your JSONString
JSONObject obj = new JSONObject(jsonStr);
JSONObject mkObj = obj.getJSONObject("mko680");
String channelId = mkObj.getString("channel_id");
Sign up to request clarification or add additional context in comments.

3 Comments

thanks , yes i've do something like that to but i want to get all data from all mko680,mkv002 and others which i dont know what is the _id is , is that any way to do it while looping ?
if you've made your JSON like that, then put my code snippet in the while loop and it'll work.
here is my full json tny.cz/72d02fc2 , can you post the loop code , i try and it just show the first one (mko680) the others not
1

Your return data is Jsonobject not an JsonArray.

So, you can create JsonObject,

JSONObject obj = new JSONObject(outputlike o);

3 Comments

thanks @rajeshwaran , first problem is solved but now there is another, see updated first post
please post your full json.
@Abdullah follow R J Answer
0

thank all based on your answer i have figured it out, at first i got difficult to get dynamic key for getting the right object i use below code to solved it, dont think its the good one but i use this in temporary

JSONObject arr = new JSONObject(bufstring);   
 Iterator keys = arr.keys();
 while(keys.hasNext()) {
  String ambilKey = (String)keys.next();
  JSONObject AmbilObject = arr.getJSONObject(ambilKey);
  Log.i("test",AmbilObject.toString());
 }

thanks for all suggestion, you're rock :D

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.