我收到以下信息 String
来自服务器的响应:
{"fid":"1272","uri":"http://someurl/services/file/1272"}
我需要将其转换为 JSONArray
。有什么帮助吗?
顺便说一句,我尝试了这个,但不起作用:
String response=getResponseFromServer();
JSONArray array = new JSONArray(response);
我收到错误:
org.json.JSONException: Value {"fid":"1272","uri":"http://someurl/services/file/1272"} of type org.json.JSONObject cannot be converted to JSONArray
最佳答案
如果您正在谈论使用 JSON in java库,那么由于您的输入字符串是 JSON 对象,而不是 JSON 数组,因此您应该首先使用 JSONObject 加载它:
String response=getResponseFromServer();
JSONObject jsonObject = new JSONObject(response);
之后,您可以使用toJSONArray()给定一个键字符串数组,将 JSONObject 转换为 JSONArray:
String[] names = JSONObject.getNames(jsonObject);
JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));
关于Java 将此字符串转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012175/