Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know the JSON parsing, but in my current project I am getting something different kind of JSON format, where name of all objects are dynamic, and they may be change.

{
    "category_id": "1",
    "parent_id": "0",
    "name": "Root Catalog",
    "is_active": null,
    "position": "0",
    "level": "0",
    "children": {
        "0": {
            "category_id": "2",
            "parent_id": "1",
            "name": "Default Category",
            "is_active": "1",
            "position": "1",
            "level": "1",
            "children": {
                "0": {
                    "category_id": "32",
                    "parent_id": "2",
                    "name": "Services",
                    "is_active": "1",
                    "position": "1",
                    "level": "2",
                    "children": {
                        "0": {
                            "category_id": "20",
                            "parent_id": "32",
                            "name": "Makeup",
                            "is_active": "1",
                            "position": "1",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "22",
                                    "parent_id": "20",
                                    "name": "Bridal Makeup",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "23",
                                    "parent_id": "20",
                                    "name": "General Makeup",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "1": {
                            "category_id": "21",
                            "parent_id": "32",
                            "name": "Skin Services",
                            "is_active": "1",
                            "position": "2",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "24",
                                    "parent_id": "21",
                                    "name": "Bleach",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "25",
                                    "parent_id": "21",
                                    "name": "Cleanup",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "26",
                                    "parent_id": "21",
                                    "name": "General Facials",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "3": {
                                    "category_id": "27",
                                    "parent_id": "21",
                                    "name": "Masks",
                                    "is_active": "1",
                                    "position": "4",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "4": {
                                    "category_id": "28",
                                    "parent_id": "21",
                                    "name": "Special Facials",
                                    "is_active": "1",
                                    "position": "5",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "5": {
                                    "category_id": "29",
                                    "parent_id": "21",
                                    "name": "Threading",
                                    "is_active": "1",
                                    "position": "6",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "6": {
                                    "category_id": "30",
                                    "parent_id": "21",
                                    "name": "Waxing",
                                    "is_active": "1",
                                    "position": "7",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "7": {
                                    "category_id": "38",
                                    "parent_id": "21",
                                    "name": "Special Services",
                                    "is_active": "1",
                                    "position": "8",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "8": {
                                    "category_id": "40",
                                    "parent_id": "21",
                                    "name": "Lip Care",
                                    "is_active": "1",
                                    "position": "9",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "9": {
                                    "category_id": "41",
                                    "parent_id": "21",
                                    "name": "Eye Care",
                                    "is_active": "1",
                                    "position": "10",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "2": {
                            "category_id": "3",
                            "parent_id": "32",
                            "name": "Body Services",
                            "is_active": "1",
                            "position": "3",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "4",
                                    "parent_id": "3",
                                    "name": "Body Wraps",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "5",
                                    "parent_id": "3",
                                    "name": "Reflexology",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "6",
                                    "parent_id": "3",
                                    "name": "Special Body Services",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "3": {
                            "category_id": "7",
                            "parent_id": "32",
                            "name": "Hair Services",
                            "is_active": "1",
                            "position": "4",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "8",
                                    "parent_id": "7",
                                    "name": "Chemical Treatments",
                                    "is_active": "0",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "42",
                                    "parent_id": "7",
                                    "name": "Hair Extensions",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "9",
                                    "parent_id": "7",
                                    "name": "Hair Color",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "3": {
                                    "category_id": "10",
                                    "parent_id": "7",
                                    "name": "Haircut",
                                    "is_active": "1",
                                    "position": "4",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "4": {
                                    "category_id": "11",
                                    "parent_id": "7",
                                    "name": "Hair Straightening",
                                    "is_active": "1",
                                    "position": "5",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "5": {
                                    "category_id": "12",
                                    "parent_id": "7",
                                    "name": "Hair Styling",
                                    "is_active": "1",
                                    "position": "6",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "6": {
                                    "category_id": "13",
                                    "parent_id": "7",
                                    "name": "Hair Treatments",
                                    "is_active": "1",
                                    "position": "7",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "7": {
                                    "category_id": "14",
                                    "parent_id": "7",
                                    "name": "Other Hair Services",
                                    "is_active": "1",
                                    "position": "8",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "8": {
                                    "category_id": "15",
                                    "parent_id": "7",
                                    "name": "Special Hair Services",
                                    "is_active": "1",
                                    "position": "9",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "4": {
                            "category_id": "16",
                            "parent_id": "32",
                            "name": "Hands And Feet",
                            "is_active": "1",
                            "position": "5",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "17",
                                    "parent_id": "16",
                                    "name": "Manicure",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "18",
                                    "parent_id": "16",
                                    "name": "Nail Art",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "19",
                                    "parent_id": "16",
                                    "name": "Pedicure",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        }
                    }
                },
                "1": {
                    "category_id": "33",
                    "parent_id": "2",
                    "name": "Memberships",
                    "is_active": "1",
                    "position": "2",
                    "level": "2",
                    "children": {

                    }
                },
                "2": {
                    "category_id": "34",
                    "parent_id": "2",
                    "name": "Packages",
                    "is_active": "1",
                    "position": "3",
                    "level": "2",
                    "children": {

                    }
                },
                "3": {
                    "category_id": "35",
                    "parent_id": "2",
                    "name": "Offers",
                    "is_active": "1",
                    "position": "4",
                    "level": "2",
                    "children": {

                    }
                },
                "4": {
                    "category_id": "36",
                    "parent_id": "2",
                    "name": "Customer Reviews",
                    "is_active": "1",
                    "position": "5",
                    "level": "2",
                    "children": {

                    }
                },
                "5": {
                    "category_id": "37",
                    "parent_id": "2",
                    "name": "Gallery",
                    "is_active": "1",
                    "position": "6",
                    "level": "2",
                    "children": {

                    }
                },
                "6": {
                    "category_id": "31",
                    "parent_id": "2",
                    "name": "Contact us",
                    "is_active": "1",
                    "position": "7",
                    "level": "2",
                    "children": {

                    }
                }
            }
        }
    }
}

So in doInBackground(), I am doing as follows:-

@Override
protected String doInBackground(String... params) {

    ServiceHandler serviceHandler = new ServiceHandler();
    String jsonStr = serviceHandler.makeServiceCall(
            Constants.SIDE_DRAWER_SERVICES, ServiceHandler.GET);
    try {
        JSONObject jobj = new JSONObject(jsonStr);
        Log.d("", "" + jsonStr.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonStr;
}

in String "jsonStr" i'm getting full data in response but when convert it into JSONObject like " JSONObject jobj = new JSONObject(jsonStr);" i'm getting missing of data like this:-

{
    "position": "0",
    "children": {
        "0": {
            "position": "1",
            "children": {
                "3": {
                    "position": "4",
                    "children": {

                    },
                    "category_id": "35",
                    "level": "2",
                    "is_active": "1",
                    "name": "Offers",
                    "parent_id": "2"
                },
                "2": {
                    "position": "3",
                    "children": {

                    },
                    "category_id": "34",
                    "level": "2",
                    "is_active": "1",
                    "name": "Packages",
                    "parent_id": "2"
                },
                "1": {
                    "position": "2",
                    "children": {

                    },
                    "category_id": "33",
                    "level": "2",
                    "is_active": "1",
                    "name": "Memberships",
                    "parent_id": "2"
                },
                "0": {
                    "position": "1",
                    "children": {
                        "3": {
                            "position": "4",
                            "children": {
                                "3": {
                                    "position": "4",
                                    "children": {

                                    },
                                    "category_id": "10",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Haircut",
                                    "parent_id": "7"
                                },
                                "2": {
                                    "position": "3",
                                    "children": {

                                    },
                                    "category_id": "9",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Color",
                                    "parent_id": "7"
                                },
                                "1": {
                                    "position": "2",
                                    "children": {

                                    },
                                    "category_id": "42",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Extensions",
                                    "parent_id": "7"
                                },
                                "0": {
                                    "position": "1",
                                    "children": {

                                    },
                                    "category_id": "8",
                                    "level": "4",
                                    "is_active": "0",
                                    "name": "Chemical Treatments",
                                    "parent_id": "7"
                                },
                                "7": {
                                    "position": "8",
                                    "children": {

                                    },
                                    "category_id": "14",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Other Hair Services",
                                    "parent_id": "7"
                                },
                                "6": {
                                    "position": "7",
                                    "children": {

                                    },
                                    "category_id": "13",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Treatments",
                                    "parent_id": "7"
                                },
                                "5": {
                                    "position": "6",
                                    "children": {

                                    },
                                    "category_id": "12",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Styling",
                                    "parent_id": "7"
                                },
                                "4": {
                                    "position": "5",
                                    "children": {

                                    },
                                    "category_id": "11",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Straightening",
                                    "parent_id": "7"
                                },
                                "8": {
                                    "position": "9",
                                    "children": {

                                    },
                                    "category_id": "15",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Special Hair Services",
                                    "parent_id": "7"
                                }
                            },
                            "category_id": "7",
                            "level": "3",
                            "is_active": "1",
                            "name": "Hair Services",
                            "parent_id": "32"
                        },
                        "2": {
                            "position": "3",
                            "children": {
                                "2": {
                                    "position": "3",
                                    "children": {

                                    },
                                    "category_id": "6",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Special Body Services",
                                    "parent_id": "3"
                                },
                                "1": {
                                    "position": "2",
                                    "children": {

                                    },

I don't know what is happening with my JSON. Please help me and tell me if JSON is invalid.

share|improve this question
    
I think your json response correct and after change it as json object its re-ordered. Its natural because of json object is un-ordered. –  Remees M Syde Dec 9 '14 at 12:00
    
So what about my missing data, how can I parse them ? –  Sanket Dec 9 '14 at 12:00
    
How do you know the datas are missing, if your use logcat for print the data it prints only some lines. –  Remees M Syde Dec 9 '14 at 12:05
    
Yes in logcate I am not getting all the JSON Objects , that's why I am confused. Please clear my confusion . –  Sanket Dec 9 '14 at 12:06
    
Try my answer below. –  Remees M Syde Dec 9 '14 at 12:14

3 Answers 3

Your logcat only dispaly some lines of data so use this function to print the data.

Call it by longInfo(your_big_string);

public static void longInfo(String str) {
    if(str.length() > 4000) {
        Log.i(TAG, str.substring(0, 4000));
        longInfo(str.substring(4000));
    } else
        Log.i(TAG, str);
}
share|improve this answer
    
Actually when I am doing debugging, then still I am not getting the missing JSON data. The main issue is this. So I am confuse that where is my missing data. Please help me. –  Sanket Dec 9 '14 at 12:16
    
try your url in browswer, is that giving all data. –  Remees M Syde Dec 9 '14 at 12:21
    
Yes sure, in browser I a getting proper reply. Actually when I am in try block and converting the string into JSON object, then my contents are missing. The statement is "JSONObject jobj = new JSONObject(jsonStr);" –  Sanket Dec 9 '14 at 12:25
    
I doesnt heard about the json block data, make sure the whole response is inside a { }, then try logcat by the above method both response and after converting json you can get some idea what is happening there. –  Remees M Syde Dec 9 '14 at 12:32
    
let me try...:) –  Sanket Dec 9 '14 at 12:33

An example JSON file to be parsed

[
    {

        "Language": "Afrikaans",
        "Native_Language": "Afrikaans",
        "code": "af"
    },

    {
        "Language": "Somali",
        "Native_Language": "af Soomaali",
        "code": "so"
    },
    {
        "Language": "Arabic",
        "Native_Language": "(al arabiya) العربية",
        "code": "ar"
    },
    {
        "Language": "Azerbaijani",
        "Native_Language": "Azərbaycan",
        "code": "az"
    }
]

and the Android code for parsing the above JSON file.This work Perfectly change it according to your json response.

package com.example.jsonparsing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

    private ListView listViewLanguages;
    ArrayList<String> languageArray  = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listViewLanguages =(ListView)findViewById(R.id.languages);
        //===========================Reading Json File===================================//
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(getAssets().open("languagesupdated.json")));
            String readData;
            while((readData = reader.readLine()) != null)
            {
                buffer.append(readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //===========================Getting Json Element===================================//
        String languageName;
        JSONArray array = null;
        try {
            array = new JSONArray(buffer.toString());
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsonObject  = array.getJSONObject(i);
                languageName = jsonObject.getString("Language");
                languageArray.add(languageName);

                }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //===========================GettingListView===================================//
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, languageArray);
          //listViewLanguages.setBackgroundColor(new ColorDrawable));
          listViewLanguages.setAdapter(adapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
share|improve this answer
up vote 3 down vote accepted

Finally got the solution, at the time of debugging, string shows all data doesn't matter how much it is, but in JSONObject all data is available but it doesn't shows while debugging.When retrieved the data from JSONObject i got all fields which are not shown in debugger.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.