简体   繁体   English

为什么此代码未在TextView中返回任何值,所以无法从json获取任何值

[英]why this code isn't returning any value in TextView cannot get any value from json

In this code the TextView field cannot have any value when pass to json and the warning is could not convert json object into json array 在此代码中,传递给json时TextView字段不能具有任何值,并且警告无法将json对象转换为json数组

String json = strListingsDetails;

        try {
            JSONArray jsonArray = new JSONArray(json);
            JSONObject jsonObject = new JSONObject(jsonArray.getJSONObject(0).toString());
            JSONArray dataListArray = jsonObject.getJSONArray("DataList");

            if (dataListArray != null && dataListArray.length() != 0) {

                for (int i = 0; i < dataListArray.length(); i++) {
                    JSONObject jsonObject1 = dataListArray.getJSONObject(i);

                    String listingId = jsonObject1.getString("ListingID");
                    String specification = jsonObject1.getString("Specifications");
                    String description = jsonObject1.getString("Description");
                    String manufacturer = jsonObject1.getString("Manufacturer");
                    String brand = jsonObject1.getString("Brand");
                    String machineType = jsonObject1.getString("ManchineType");
                    String condition = jsonObject1.getString("Condition");
                    String yearOfMfg = jsonObject1.getString("YearofMfg");
                    String Price=jsonObject1.getString("Price");
                    String ShowPrice=jsonObject1.getString("ShowPrice");
                    String country = jsonObject1.getString("Country");
                    String state = jsonObject1.getString("State");
                    String city = jsonObject1.getString("City");
                    String qty = jsonObject1.getString("Qty");
                    String height = jsonObject1.getString("Height");
                    String width = jsonObject1.getString("Width");
                    String length = jsonObject1.getString("Length");
                    String weight = jsonObject1.getString("Weight");
                    String warranty = jsonObject1.getString("Warranty");
                    String expiredOn = jsonObject1.getString("ExpiredOn");

could not convert json object into json array 无法将json对象转换为json数组

You are trying to parse JSON string which is JSON Object and not an Array 您正在尝试解析JSON字符串,它是JSON对象而不是数组

Try like this. 尝试这样。

JSONObject jsonObject = new JSONObject(json);
JSONArray dataListArray = jsonObject.getJSONArray("DataList");

please match your json as below what i guess: 请按照我的猜测匹配您的json:

[
{
    "DataList": [
        {
            "ListingID": "value",
            "Specifications": "value",
            "Description": "value",
            "Manufacturer": "value",
            "Brand": "value"
             ...
        }
    ]
}
]

log your json, you will be find the answer 记录您的json,您将找到答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM