1

Android parse JSONObject

i have searched questions like that but couldnot get success.

I have JSON data lke that http://oep.esy.es/item_connect.php

{
   "items":[
      {
         "id":"7",
         "name":"bir",
         "image":"6051-1.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"8",
         "name":"d\u00f6rt",
         "image":"1727-4.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"9",
         "name":"iki",
         "image":"2667-2.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"10",
         "name":"\u00fc\u00e7",
         "image":"9998-3.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"11",
         "name":"be\u015f",
         "image":"7005-5.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"12",
         "name":"alt\u0131",
         "image":"9377-6.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"13",
         "name":"elma",
         "image":"1656-1001.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"k\u0131rm\u0131z\u0131"
      }
   ],
   "success":1
}

At first there is word of items and at last there is word of success. There are out of array. WHen i delete them, i can success.

StringBuilder sb = new StringBuilder();

sb.append(json+"\n");//Log.d(TAG, "sb "+sb.toString());
}

try {Log.d(TAG, "xx ");
      JSONArray mJsonArray = new JSONArray(sb);Log.d(TAG, "xx x");
      JSONObject mJsonObject = new JSONObject();

Here it cant go xx x log, it goes into error catch.

but when i delete header and footer form json, it works. Should i use string operations to delete those or regex?

Edit:

i solved like this:

 try {Log.d(TAG, "xx ");
       JSONArray(sb);Log.d(TAG, "xx x");
                    JSONObject mJsonObject = new JSONObject();//i use this to get invidial value of json, do i need?

                    JSONObject jsono = new JSONObject(sb.toString());

                    Log.d(TAG,"jsono "+jsono.toString());

                    JSONArray items = jsono.getJSONArray("items");


                    Log.d(TAG,"items "+items);
                    Log.d(TAG,"items length "+items.length());

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


                           //   Log.d(TAG, "xx " +mJsonObject.getString("image"));
                               Log.d(TAG, "string: "+items.getJSONObject(i).getString("image"));//i need image urls
                               }

This code does it job but sometimes it goes to error of try catch and i cant see any errors. and sometimes it takes very long time, around 1-2 minutes and mostly n0t fail in the end. why can it be?

Community
  • 1
  • 1

5 Answers5

0

The whole response is a JSONObject, so parse like this instead:

JSONObject jsono = new JSONObject(json);

The you can pull items and success out of it:

JSONArray items = jsono.getJSONArray("items");
int success = jsono.getInt("success");
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • This code does it job but sometimes it goes to error of try catch and i cant see any errors. and sometimes it takes very long time, around 1-2 minutes and mostly n0t fail in the end. why can it be? –  Apr 09 '16 at 18:21
0

use this code

Gson mGson = new Gson(); mGson.fromJson(jsonResponse.getJSONObject(i).toString(),DataChartDetailsInquiry.class);

Ashkan
  • 1,357
  • 4
  • 16
  • 37
0

Try this way to parse your json

public class Modelclass {

    String id;
    String name;
    String image;
    String number;
    String shape;
    String color;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

JSON parse code

       ArrayList<Modelclass> arrayList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonstring);
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            Modelclass modelclass;
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json_child_obje = jsonArray.getJSONObject(i);
                modelclass = new Modelclass();
                modelclass.setId(json_child_obje.getString("id"));
                modelclass.setId(json_child_obje.getString("name"));
                modelclass.setId(json_child_obje.getString("image"));
                modelclass.setId(json_child_obje.getString("number"));
                modelclass.setId(json_child_obje.getString("shape"));
                modelclass.setId(json_child_obje.getString("color"));
                arrayList.add(modelclass);
            }
            Log.d("ArraylistSize", "" + arrayList.size());
        } catch (JSONException e) {
            e.printStackTrace();
        }
Pravin Fofariya
  • 346
  • 2
  • 11
0

First Create arraylist to store yout data

ArrayList<List_items> list_items_arr = new ArrayList<List_items>();

Create class for Lisr_items

      public class List_items{
       String item_id,item_name;
    public void setItem_id(String item_id) {
            this.item_id = item_id;
        }

   public void setItem_name(String item_name) {
        this.item_name = item_name;
      }
  }

get the response from server and parse it

JSONObject jsono = new JSONObject(json);

JSONArray items = jsono.getJSONArray("items");

  for(int i=0;i<items.length();i++){
         JSONObject list_obj=items.getJSONObject(i);
            List_items list_items = new List_items();
            list_items.setItem_name(list_obj.getString("id"));
            list_items.setItem_sku(list_obj.getString("name"));
            list_items.setItem_id(list_obj.getString("image"));
     list_items_arr .add(list_items);
 }
Rgv
  • 504
  • 5
  • 23
  • why keeping it as json data and when needed, taking data from json? –  Apr 09 '16 at 18:21
0

replace your JSONArray mJsonArray = new JSONArray(sb) code

with

JSONObject jsnobject = new JSONObject(sb);

because your json data First tag is JSONObject not JSONArray , try it will work for you

Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • This code does it job but sometimes it goes to error of try catch and i cant see any errors. and sometimes it takes very long time, around 1-2 minutes and mostly n0t fail in the end. why can it be? –  Apr 09 '16 at 18:21