0

how can parsing this output

{
    "durum": "tamam",
    "mahalleler": [
        {
            "mahalle_kodu": "1",
            "mahalle_ismi": "BEKTAŞ MAH."
        },
        {
            "mahalle_kodu": "2",
            "mahalle_ismi": "ÇARŞI MAH."
        }]}

i try this code but return "null".

            contacts = json.getJSONArray("mahalleler");

            for (int i = 0; i < contacts.length(); i++) {

                JSONObject c = contacts.getJSONObject(i);

                String name = c.getString("mahalle_kodu");
                String body = c.getString("mahalle_ismi");}

what is the problem? json output has two variable; mahalleler[] and durum. i wanna parse durum's valur and values of mahalleler array. But i couldnt do that.

2
  • check this tutorial androidhive.info/2012/01/android-json-parsing-tutorial Commented Aug 23, 2012 at 13:16
  • Where are you trying to output the data? Defining your variables in your for loop makes them local to that loop. Try creating class variables and assigning them value. Commented Aug 23, 2012 at 13:17

4 Answers 4

1
      JSONObject json = new JSONObject("{
"durum": "tamam",
"mahalleler": [
    {
        "mahalle_kodu": "1",
        "mahalle_ismi": "BEKTAŞ MAH."
    },
    {
        "mahalle_kodu": "2",
        "mahalle_ismi": "ÇARŞI MAH."
    }]}
   ");

 String name[]
    String body[]

      JSONArray  contacts = json.getJSONArray("mahalleler");
         name=new String[contacts.length()];
      body  =new String[contacts.length()];

        for (int i = 0; i < contacts.length(); i++) {

            JSONObject c = contacts.getJSONObject(i);

             name[i] = c.getString("mahalle_kodu");
             body[i] = c.getString("mahalle_ismi");}

i think u r nt geting o/p as u declared the strings inside the for loop

0

You hve to do get json array like this

JSONArray mahallelerArray = c.getJSONArray("mahalleler");
0

you need to check if the content is in the json:

if (json != null && !json.isNull("mahalleler")) {
   JSONArray contacts = json.getJSONArray("mahalleler");

        for (int i = 0; i < contacts.length(); i++) {

            JSONObject c = contacts.getJSONObject(i);

            String name = c.getString("mahalle_kodu");
            String body = c.getString("mahalle_ismi");}

}
0

You are declaring your String inside your for loop. Make them class variables arrays to store the data.

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.