我想在尝试将JSON OBJ转换为数组时使用json以表格格式显示数据

时间:2016-05-30 12:12:59

标签: android json

我正在尝试将JSONObject转换为JSONArray,但存在一些问题。我的代码是:

public ArrayList<Users> parseJSON(String result) {
    ArrayList<Users> users = new ArrayList<Users>();
    try {
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            Users user = new Users();
            user.setId(json_data.getInt("cus_id"));
            user.setName(json_data.getString("cus_Name"));
            user.setPlace(json_data.getString("cus_city"));
            users.add(user);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return users;
}

logcat的:

05-30 17:30:16.152 5956-5956/com.example.vari.table_format E/log_tag:
Error parsing data org.json.JSONException: Value
{"Customer_mas":[{"Id":8,"cus_id":"CUS0001","cus_Name":"SUNRISE ELECTRONICS","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  7:20AM","Updated_date":"Nov 20 2013  7:20AM"},{"Id":9,"cus_id":"CUS0002","cus_Name":"LGB VAYAMPALAYAM","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  7:56AM","Updated_date":"Nov 20 2013  7:56AM"},{"Id":10,"cus_id":"CUS0003","cus_Name":"LGB ANNUR","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  7:56AM","Updated_date":"Nov 20 2013  7:56AM"},{"Id":31,"cus_id":"CUS0024","cus_Name":"INTELLE ENGG","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  8:07AM","Updated_date":"Nov 20 2013  8:07AM"},{"Id":32,"cus_id":"CUS0025","cus_Name":"JAAZ ELECTRO","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  8:07AM","Updated_date":"Nov 20 2013  8:07AM"},{"Id":33,"cus_id":"CUS0026","cus_Name":"JEEVAN ELECTRONICS","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  8:07AM","Updated_date":"Nov 20 2013  8:07AM"},{"Id":34,"cus_id":"CUS0027","cus_Name":"J.K.R ENGINERS","cus_city":"COIMBATORE","Created_date":"Nov 20 2013  8:07AM","Updated_date":"Nov 20 2013  8:07AM"}]} of type org.json.JSONObject cannot be converted to JSONArray

JSON回复:

{
   "Customer_mas":[
      {
         "Id":8,
         "cus_id":"CUS0001",
         "cus_Name":"SUNRISE ELECTRONICS",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 7:20AM",
         "Updated_date":"Nov 20 2013 7:20AM"
      },
      {
         "Id":9,
         "cus_id":"CUS0002",
         "cus_Name":"LGB VAYAMPALAYAM",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 7:56AM",
         "Updated_date":"Nov 20 2013 7:56AM"
      },
      {
         "Id":10,
         "cus_id":"CUS0003",
         "cus_Name":"LGB ANNUR",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 7:56AM",
         "Updated_date":"Nov 20 2013 7:56AM"
      },
      {
         "Id":31,
         "cus_id":"CUS0024",
         "cus_Name":"INTELLE ENGG",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 8:07AM",
         "Updated_date":"Nov 20 2013 8:07AM"
      },
      {
         "Id":32,
         "cus_id":"CUS0025",
         "cus_Name":"JAAZ ELECTRO",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 8:07AM",
         "Updated_date":"Nov 20 2013 8:07AM"
      },
      {
         "Id":33,
         "cus_id":"CUS0026",
         "cus_Name":"JEEVAN ELECTRONICS",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 8:07AM",
         "Updated_date":"Nov 20 2013 8:07AM"
      },
      {
         "Id":34,
         "cus_id":"CUS0027",
         "cus_Name":"J.K.R ENGINERS",
         "cus_city":"COIMBATORE",
         "Created_date":"Nov 20 2013 8:07AM",
         "Updated_date":"Nov 20 2013 8:07AM"
      }
   ]
}

3 个答案:

答案 0 :(得分:1)

试试这个:

   JSONObject obj = new JSONObject(result);
   JSONArray jArray = obj.getJSONArray("Customer_mas");
    for (int i = 0; i < jArray.length(); i++) {
        JSONObject json_data = jArray.getJSONObject(i);
        Users user = new Users();
        user.setId(json_data.getString("cus_id"));
        user.setName(json_data.getString("cus_Name"));
        user.setPlace(json_data.getString("cus_city"));
        users.add(user);

答案 1 :(得分:1)

像这样分析您的数据。

public ArrayList<Users> parseJSON(String result) {
    ArrayList<Users> userList  = new ArrayList<>();
    try {
        JSONObject jsonObject = new JSONObject(result);
        JSONArray jsonArray = jsonObject.getJSONArray("Customer_mas");
        for(int i =0; i<jsonArray.length();i++){
            JSONObject jsonData = jsonArray.getJSONObject(i);
            Users user = new Users();
            user.setId(jsonData.getInt("id"));
            user.setCus_id(jsonData.getString("cus_id"));
            user.setName(jsonData.getString("cus_Name"));
            user.setPlace(jsonData.getString("cus_city"));
            userList.add(user);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return userList;
}

如果id的POJO对象类型是字符串,那么它肯定会起作用。

将此代码粘贴到Users.java类

public class Users {
String cus_id;
String name;
String place;
int id;


public Users() {
    this.cus_id = "";
    this.name = "";
    this.place = "";
    this.id = 0;

}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}
public String getCus_id() {
    return cus_id;
}

public void setCus_id(String cus_id) {
    this.cus_id = cus_id;
}

public String getName() {
    return name;
}

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

public String getPlace() {
    return place;
}

public void setPlace(String place) {
    this.place = place;
}

}

答案 2 :(得分:0)

 public void addData(ArrayList<Users> users) {

    addHeader();

    for (Iterator i = users.iterator(); i.hasNext();) {

        Users p = (Users) i.next();

        /** Create a TableRow dynamically **/
        tr = new TableRow(this);

        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText(p.getName());
        label.setId(p.getCus_id()); //here is the problem 
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        label.setBackgroundColor(Color.GRAY);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 2, 2, 2);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label,params);
        tr.addView((View)Ll); // Adding textView to tablerow.

        /** Creating Qty Button **/
        TextView place = new TextView(this);
        place.setText(p.getPlace());
        place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place.setPadding(5, 5, 5, 5);
        place.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 2, 2, 2);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place,params);
        tr.addView((View)Ll); // Adding textview to tablerow.

        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
}

label.setId(p.getCus_id()); //这是问题