0

`Hi Guys,

Kindly check format of this Json.

{"id": "293369****","owner": {
  "name": "******",
  "id": "*****"},name": "test event","start_time": "2012-05-18T02:00:00","end_time":"2012-05-18T05:00:00","privacy": "FRIENDS","updated_time": "2012-05-18T08:25:37+0000"}

M using this code for it. M able to fetch all values except owner json array. I want to fetch value of owner array. Kindly suggest me where i am going wrong.

json = new JSONObject(responsedata);
        Log.d(TAG, "name=" + responsedata);
        facebookData = new Events();
        JSONArray data=json.optJSONArray("owner");

        Log.d(TAG, "Data length==" + data.length());
        if (data.length() > 0) {
            for (int i = 0; i < data.length(); i++) {

                JSONObject jsonobj = data.getJSONObject(0);
                Log.d(TAG, "owner Name="+jsonobj.getString("name"));
            }
            }

        facebookData.setEventName(json.getString("name"));
        facebookData.setEventId(json.getString("id"));
        facebookData.setEventStartDate(json.getString("start_time"));
        facebookData.setEventEndDate(json.getString("end_time"));
        facebookData.setPrivacy(json.getString("privacy"));
    facebookData.setUpdatedTime("updated_time");

I am unable to fetch owner name and owner facebook id. There is some problem in my json parser.

Kindly help me in this.

Thanks in Advance Gaurav Gupta

0

3 Answers 3

5
  {
   "id":"293369****",
   "owner":{
      "name":"******",
      "id":"*****"
   },
   "name":"test event",
   "start_time":"2012-05-18T02:00:00",
   "end_time":"2012-05-18T05:00:00",
   "privacy":"FRIENDS",
   "updated_time":"2012-05-18T08:25:37+0000"
 }

As { implies JsonObject and [ implies JsonArray.

I dont find any JsonArray in the above string. "owner" is JsonObject.

You have to modify your code see http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

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

1 Comment

thax, i got ur point. thax +1 for this concept and quick help. its work.
0
try this one
 try{
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data=null;
           for(int i=0;i<jArray.length();i++)
           {
                json_data =jArray.getJSONObject(i);
                String src_name=json_data.getString("owner");


           }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
            Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
    }

Comments

0

if owner is a JSONArray, it should look like this (inside [])

"owner": [{"name": "******", "id": "*****"}]

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.