小编典典

Android-如何从字符串解析JsonArray?

json

我试图从json字符串解析json数组,但它总是抛出异常data of type java.lang.String cannot be converted to JSONArray

如果我有任何错误,请告诉我。

谢谢。

这是我从服务器获取Json的代码:

try {
                String url = String.format(<url here>, province.provinceCode2);
                HttpClient httpClient = getHttpClient();
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity entity = httpResponse.getEntity();
                final String result = EntityUtils.toString(entity);
                parseAndSaveJsonData(province, result);
            } catch (Exception e) {
                e.printStackTrace();
            }

这是解析JsonArray的代码:

String jsonString = <below json string>
JSONArray ja = new JSONArray(jsonString);

这是我的json字符串:

   [
   {
      "LotPrizes":[
         {
            "Prize":"Giảitám",
            "Range":"50"
         },
         {
            "Prize":"Giảibảy",
            "Range":"264"
         },
         {
            "Prize":"Giảisáu",
            "Range":"3654-5162-3097"
         },
         {
            "Prize":"Giảinăm",
            "Range":"9739"
         },
         {
            "Prize":"Giảitư",
            "Range":"97690-99274-32442-69432-04855-10132-17085"
         },
         {
            "Prize":"Giảiba",
            "Range":"73745-13007"
         },
         {
            "Prize":"Giảinhì",
            "Range":"05521"
         },
         {
            "Prize":"Giảinhất",
            "Range":"74870"
         },
         {
            "Prize":"GiảiDB6",
            "Range":"878833"
         }
      ]
      },
 {
      "LotPrizes":[
         {
            "Prize":"Giảitám",
            "Range":"50"
         },
         {
            "Prize":"Giảibảy",
            "Range":"264"
         },
         {
            "Prize":"Giảisáu",
            "Range":"3654-5162-3097"
         },
         {
            "Prize":"Giảinăm",
            "Range":"9739"
         },
         {
            "Prize":"Giảitư",
            "Range":"97690-99274-32442-69432-04855-10132-17085"
         },
         {
            "Prize":"Giảiba",
            "Range":"73745-13007"
         },
         {
            "Prize":"Giảinhì",
            "Range":"05521"
         },
         {
            "Prize":"Giảinhất",
            "Range":"74870"
         },
         {
            "Prize":"GiảiDB6",
            "Range":"878833"
         }
      ]
      }

    ]

阅读 305

收藏
2020-07-27

共1个答案

小编典典

嗨,@ Caerulius,Harish,ρяσsρєяK,Hot Licks等等。
最终,经过2天的头痛和2个不眠之夜,我解决了这个问题。而且由于您花了宝贵的时间与我讨论,因此我认为我必须告诉您根本原因。那是我的责任

首先,我是一名高级android开发人员。因此,我至少了解JSON基本知识,知道如何从JSON字符串解析数据,并且知道许多有用的在线工具来进行验证。我确认从服务器获取的JSON字符串有效。

就像我在问题中告诉我的那样,我曾经final String result = EntityUtils.toString(entity);HttpEntity对象获取JSON字符串。过去,我已经使用过很多次了,并且有效。没问题。但是,在这种情况下不是。原始的JSON字符串如下所示:

   [{
      "LotPrizes":[
         {
            "Prize":"Giảitám",
            "Range":"50"
         },
         {
            "Prize":"Giảibảy",
            "Range":"264"
         },
         ...
    }]

但是我得到的是这样的:

"[{
      \"LotPrizes\":[
         {
            \"Prize":\"Giảitám\",
            \"Range\":\"50\"
         },
         {
            \"Prize\":\"Giảibảy\",
            \"Range\":\"264\"
         },
         ...
    }]"

该字符串与常量字符串类似,我们可以如下声明:

String stringVariable = "\"[{
          \\\"LotPrizes\\\":[
             {
                \\\"Prize":\\\"Giảitám\\\",
                \\\"Range\\\":\\\"50\\\"
             },
             {
                \\\"Prize\\\":\\\"Giảibảy\\\",
                \\\"Range\\\":\\\"264\\\"
             },
             ...
        }]\" ;

这是有效的字符串,但不是有效的JSON字符串。

为了解决此问题,我更改了获取JSON字符串的方式,并删除了不必要的字符,如下所示:

                            HttpClient httpClient = getHttpClient();
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity entity = httpResponse.getEntity();
                InputStream is = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                String json = sb.toString();

                json = json.replace("\\", "");
                json = json.substring(1);
                json = json.substring(0, json.length() - 2);

现在,该json变量包含我可以正确解析的JSON字符串。我认为这应该是HttpEntity库的错误。

希望这对其他人有所帮助。

2020-07-27