本文介绍了资源下载转换该字符串JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到从服务器以下字符串
响应:
{裂:1272,URI:HTTP:// someurl /服务/文件/ 1272}
我需要将其转换成 JSONArray
。任何帮助吗?
顺便说一句,我尝试这样做,这是行不通的:
字符串响应= getResponseFromServer();
JSONArray阵列=新JSONArray(响应);
我得到的错误:
org.json.JSONException:价值型组织的{裂:1272,URI:// someurl /服务/文件/ HTTP 1272}。 json.JSONObject不能转换为JSONArray
解决方案
如果你在谈论使用库,那么因为你的输入字符串是一个JSON对象,而不是一个JSON数组,你应该先使用的JSONObject加载:
字符串响应= getResponseFromServer();
的JSONObject的JSONObject =新的JSONObject(响应);
在这之后,你可以使用toJSONArray()以一个JSONObject转换成JSONArray定的密钥字符串数组:
的String [] =名称JSONObject.getNames(JSONObject的);
JSONArray jsonArray = jsonObject.toJSONArray(新JSONArray(地名));
I am getting the following String
response from a server:
{"fid":"1272","uri":"http://someurl/services/file/1272"}
I need to convert it into a JSONArray
. Any help?
By the way, I tried this and it does not work:
String response=getResponseFromServer();
JSONArray array = new JSONArray(response);
I get the error:
org.json.JSONException: Value {"fid":"1272","uri":"http://someurl/services/file/1272"} of type org.json.JSONObject cannot be converted to JSONArray
解决方案
If you're talking about using the JSON in java library, then since your input string is a JSON object, not a JSON array, you should first load it using JSONObject:
String response=getResponseFromServer();
JSONObject jsonObject = new JSONObject(response);
After that, you can use toJSONArray() to convert a JSONObject to JSONArray given an array of key strings:
String[] names = JSONObject.getNames(jsonObject);
JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));
这篇关于资源下载转换该字符串JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!