问题描述
我试图解析来自JSON字符串一个JSON数组,但它总是抛出异常 java.lang.String类型的数据不能转换成JSONArray
。
请告诉我,如果我犯任何错误。
感谢。
下面是我的codeS从服务器获取的Json:
尝试{
字符串URL =的String.Format(小于URL这里>中province.province code2);
HttpClient的HttpClient的= getHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
HTT presponse HTT presponse = httpClient.execute(HTTPGET);
HttpEntity实体= HTT presponse.getEntity();
最后的字符串结果= EntityUtils.toString(实体);
parseAndSaveJsonData(省,结果);
}赶上(例外五){
e.printStackTrace();
}
下面是codeS解析JsonArray:
字符串jsonString =<低于JSON字符串>
JSONArray JA =新JSONArray(jsonString);
下面是我的JSON字符串:
[
{
LotPrizes:[
{
奖:Giảitám
范围:50
},
{
奖:Giảibảy
范围:264
},
{
奖:Giảisáu
范围:3654-5162-3097
},
{
奖:Giảinăm
范围:9739
},
{
奖:Giảitư
范围:97690-99274-32442-69432-04855-10132-17085
},
{
奖:Giảiba
范围:73745-13007
},
{
奖:Giảinhì
范围:05521
},
{
奖:Giảinhất
范围:74870
},
{
奖:GiảiDB6
范围:878833
}
]
},
{
LotPrizes:[
{
奖:Giảitám
范围:50
},
{
奖:Giảibảy
范围:264
},
{
奖:Giảisáu
范围:3654-5162-3097
},
{
奖:Giảinăm
范围:9739
},
{
奖:Giảitư
范围:97690-99274-32442-69432-04855-10132-17085
},
{
奖:Giảiba
范围:73745-13007
},
{
奖:Giảinhì
范围:05521
},
{
奖:Giảinhất
范围:74870
},
{
奖:GiảiDB6
范围:878833
}
]
}
]
@Caerulius哈里什,ρяσѕρєяK,热舔,以及所有。最后,术后第2天头痛和2个不眠之夜,我解决了这个问题。而且因为你花你的价值的时间和我商量,我看到,我要告诉你的根本原因。这是我的责任。的
首先,我是一名资深的Android开发者。所以,我至少知道JSON的基础,我知道如何解析来自JSON字符串的数据,我知道了许多有用的在线工具来验证它。我确认JSON字符串,我从服务器得到有效。
正如我告诉我的问题,我用最后的字符串结果= EntityUtils.toString(实体);
得到JSON字符串 HttpEntity
对象。我在通用很多次,它的工作。没问题。但是,在这种情况下,它不是。原来的JSON字符串是这样的:
[{
LotPrizes:[
{
奖:Giảitám
范围:50
},
{
奖:Giảibảy
范围:264
},
...
}]
但我得到这样的:
[{
\LotPrizes \:[
{
\奖:\Giảitám\,
\范围\:\50 \
},
{
\奖\:\Giảibảy\,
\范围\:\264 \
},
...
}]
这串与字符串常量,我们可以宣布为类似如下:
字符串stringVariable =\[{
\\\LotPrizes \\\:
{
\\\奖:\\\Giảitám\\\
\\\范围\\\:\\\50 \\\
},
{
\\\奖\\\:\\\Giảibảy\\\
\\\范围\\\:\\\264 \\\
},
...
}] \;
这是一个有效的字符串,而不是一个有效的JSON字符串。
要解决这个问题,我改变方式来获得JSON字符串,并删除不必要的字符是这样的:
的HttpClient的HttpClient = getHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
HTT presponse HTT presponse = httpClient.execute(HTTPGET);
HttpEntity实体= HTT presponse.getEntity();
InputStream的是= entity.getContent();
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,UTF-8),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
is.close();
JSON字符串= sb.toString();
JSON = json.replace(\\,);
JSON = json.substring(1);
JSON = json.substring(0,json.length() - 2);
现在,在 JSON
变量包含JSON字符串,我可以正确地解析。我想这应该HttpEntity库的一个bug。
希望这有助于一些其他球员。
I am trying to parse a json array from json string but it always throws the exception data of type java.lang.String cannot be converted to JSONArray
.
Please tell me if I make any mistake.
Thanks.
Here is my codes to get Json from server:
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();
}
here is codes to parse JsonArray:
String jsonString = <below json string>
JSONArray ja = new JSONArray(jsonString);
Here is my json string:
[
{
"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"
}
]
}
]
Hi @Caerulius, Harish, ρяσѕρєя K, Hot Licks , and all.Finally, after 2 days of headache and 2 sleepless nights, I solved the issue. And because you spend your valued time to discuss with me, I see that I have to tell you the root cause. That's my responsibility.
First of all, I am a senior android developer. So, I at least know about JSON basic, I know how to parse data from JSON string, and I know many useful online tools to validate it. I confirm that the JSON string I got from server is valid.
As I told in my question, I used final String result = EntityUtils.toString(entity);
to get JSON string from HttpEntity
object. I have used this many times in the pass and it worked. No problem. But, in this case, it's not.The original JSON string like this:
[{
"LotPrizes":[
{
"Prize":"Giảitám",
"Range":"50"
},
{
"Prize":"Giảibảy",
"Range":"264"
},
...
}]
But what I got like this:
"[{
\"LotPrizes\":[
{
\"Prize":\"Giảitám\",
\"Range\":\"50\"
},
{
\"Prize\":\"Giảibảy\",
\"Range\":\"264\"
},
...
}]"
This string is similar with the constant string which we may declare as below:
String stringVariable = "\"[{
\\\"LotPrizes\\\":[
{
\\\"Prize":\\\"Giảitám\\\",
\\\"Range\\\":\\\"50\\\"
},
{
\\\"Prize\\\":\\\"Giảibảy\\\",
\\\"Range\\\":\\\"264\\\"
},
...
}]\" ;
It's a valid string, but not a valid JSON String.
To fix this issue, I change the way to get JSON string, and remove unnecessary characters like this:
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);
Now, the json
variable contain JSON string which I can parse correctly. I think this should a bug of HttpEntity library.
Hope this helps some other guys.
这篇关于Android的 - 如何从字符串解析JsonArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!