我正在尝试从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"
}
]
}
]
答案 0 :(得分:15)
这是初始化JSON解析器的方法:
JSONObject jsonObject = new JSONObject(jsonString);
这将为您提供整个字符串作为Json对象。从那里,拉出一个单独的数组作为JsonArray,如下所示:
JSONArray jsonArray = jsonObject.getJSONArray("LotPrizes");
要访问每个“LotPrizes”,您可以使用循环逻辑:
for(int i=0;i<jsonArray.length();i++)
{
JSONObject curr = jsonArray.getJSONObject(i);
prize = curr.getString("Prize")
//Do stuff with the Prize String here
//Add it to a list, print it out, etc.
}
编辑: JSON编辑后的最终代码:
JSONArray jsonArray = null;
String jsonString = <your string>
String currPrize = null;
JSONObject jsonObject = new JSONObject(jsonString);
jsonArray = jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++)
{
JSONArray currLot = jsonArray.getJSONObject(i);
for(int j=0; j<currLot.length();j++)
{
JSONobject curr = currLot.getJSONObject(j);
currPrize = curr.getString("Prize");
//Do something with Prize
}
}
此代码功能正常,我在代码中使用的版本几乎相同。希望这(终于)适合你。
答案 1 :(得分:2)
你好@Caerulius,Harish,ρяσѕρєя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库的一个bug。
希望这有助于其他人。
答案 2 :(得分:1)
您可以从字符串中检索jsondata,如下所示..
JSONObject json = new JSONObject(jsonString);
JSONArray jData = json.getJSONArray("data");
for (int i = 0; i < jData.length(); i++) {
JSONObject jo = jData.getJSONObject(i);
JSONArray jLotPrizes = jo.getJSONArray("LotPrizes");
for (int j = 0; j < jLotPrizes.length(); j++) {
JSONObject jobj = jLotPrizes.getJSONObject(j);
Log.i("Prize", "" + jobj.getString("Prize"));
Log.i("Range", "" + jobj.getString("Range"));
}
}