[英]Error while Converting to Json Object
I am working with restful webservice and I get json from it like the following 我正在使用restful webservice,我从中得到json,如下所示
response = [{"id":1,"name":"Appetizers","image":"iVBORw0KGgoAAAANSUhEUgAAADgAAAAkCAIAAABT8G6pAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABb5SURBVFhHPVgHUFPrtmbevTPvvnPvO8eKKJ1ASAikEEIaSYBAEkroSLWBHex67A3FBhYsWEFUBBuCDRRRpCMlhPS+0zs1gHD0HN72vjfvnzV/dpI9s7 ....
after that I want to convert this json to JSONObject 之后我想将这个json转换为JSONObject
JSONObject obj = new JSONObject(response);
and when I execute this command I get the follwoing error 当我执行这个命令时,我得到了以下错误
org.json.JSONException: Value [{"resturantID":{"phoneNo":"a","id":1,"fax":"a","address":"a" ...
my program was working good when I was using glassfish 3 because the json structure was different from this what is the problem ? 当我使用glassfish 3时,我的程序运行良好,因为json结构与此不同是什么问题?
This is a JSONArray
and not a JSONObject
- to make a JSONObject from it, use 这是一个
JSONArray
而不是JSONObject
- 从中创建一个JSONObject,使用
JSONObject jsonObject = jsonArray.getJSONObject(0);
this gets the first JSONObject from this JSONArray. 这从这个JSONArray获取第一个JSONObject。
If you have multiple JSONObjects, use this: 如果您有多个JSONObjects,请使用:
JSONObject jsonObject;
for(int n = 0; n < jsonArray.length(); n++)
{
jsonObject = jsonArray.getJSONObject(n);
}
if your response
hold the output of the stringbuilder after iterations then you can try below code 如果您的
response
在迭代后保持stringbuilder的输出,那么您可以尝试下面的代码
try{
jArray = new JSONArray(response);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
}
}
NOTE:do not forget to declare jArray
(object of JSONARRAY) and json_data
(object of JSONOBJECT) at class level 注意:不要忘记在类级别声明
jArray
( jArray
对象)和json_data
( json_data
对象)
I think, problem is on your JSON. 我想,问题在于你的JSON。 JSON is not valid.
JSON无效。
"id":1
it should be 它应该是
"id":"1"
Please check. 请检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.