- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Android 中有一个项目,我这里有这个 JSON,我需要解析它以便我可以使用数据:(我已经尝试在 JSONLint.com 上验证我的 JSON,一切正常)
{
"traces": [
"{\"lat\": \"42.842097\", \"lng\": \"-31.258422\", \"idTrace\": \"444e8079\"}",
"{\"lat\": \"56.842097\", \"lng\": \"-73.258422\", \"idTrace\": \"088b591a\"}"
]
}
问题是,当我尝试获取 JSONArray 中的 2 个 JSONObject 时,我总是遇到错误。
这是我的代码 fragment :
(...)
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;)
{
builder.append(line).append("\n");
}
JSONObject result = new JSONObject(builder.toString());
JSONArray list = result.getJSONArray("traces");
try{
for (int i = 0; i < list.length(); i++)
{ //this is where I get the error "JSONArray[0] is not a JSONObject"
JSONObject jsonObject = list.optJSONObject(i);
//String id = jsonObject.getString("idTrace");
//String lat = jsonObject.getString("lat");
//String lng = jsonObject.getString("lng");
(...)
}
(...)
我进行了大量搜索以找出问题所在,但人们似乎在做与我完全相同的事情...我找不到它不起作用的原因!
感谢您的帮助!
最佳答案
此 JSON 是否从您控制的 API 返回?我认为破坏的是 JSONArray 中项目周围的引号 - JSON 应如下所示:
{"traces": [
{"lat": "42.842097", "lng": "-31.258422", "idTrace": "444e8079"},
{"lat": "56.842097", "lng": "-73.258422", "idTrace": "088b591a"}
]}
请注意,每个 JSONObject 都没有引号。你现在拥有的更像是一个字符串数组......
关于安卓 : JSONArray[0] is not a JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464727/
我正在使用网络服务请求直方图数据。数据是数组中的一组数组: [[1375056000000,23.284713745117],[1375142400000,3.809531211853], [1375
这是我的 json 文件的相关部分: "categories": [ [ "Belgian Restaurant", "belgian" ],
我目前很困惑,为什么我不能在我的 Android 应用程序中从 JSONArray 中提取 JSONArray。下面给出了一个示例和我的源代码片段。 //The JSON { "cur
我正在尝试解析 JSONObject。 这个 JSONObject 中有一个 JSONArray,它在 JSONArray 中还有另一个 JSONArray。 我试图解析的 json 形式如下。 {
我正在尝试解析对象数据列表中的 JSON 数组 payment_details。 在这里,我已经成功解析了回收站 View 中调用的数据数组,但我无法调用 payment_details 数组。 适配
我正在寻找一个选项,可以将多个值添加到 JSONArray 并将其添加到另一个 JSONArray 中,而无需创建多个变量。例如: JSONArray array1 = new JSONArr
我遇到过一种情况,org.json.JSONArray 对象非常大,这最终会导致延迟和其他问题。因此,我们决定将 JSONArray 分割成更小的 block 。例如,如果 JSONArray 是这样
您好,我正在尝试在另一个 json 数组中解析一个 json 数组。帮我解决这个问题。 我的 Json 数组是 "results":[ { "percentcompleted"
我正在使用 Gson用于解析 json 响应。我需要在 JsonArray 中解析 JSONArry.. 我的回答是.. { "message": "Retreive sucessfully", "f
@GET @Produces("application/json") public Response GetAll() throws JSONException{ tblCategoryDao
我从服务器获取以下 json: { "Data": [ { "Record": [ " d11", "d12"
我正在使用 ajax 将 json 对象从 javascript 传递到 java servlet。 var jsonObj = JSON.stringify(objArray); //Then I
我需要获取作为“结果”内部数组的“示例”数据 JSON FILE 并将其附加到字符串。最简单的方法是什么? 这就是我得到“定义”的方式 private JSONObject queryResults;
我需要从 JSONArray 获取 JSONArray: JSONParser parser = new JSONParser(); JSONObject jObject=(J
我有一个 jsonArray 响应。我需要阅读此响应,其中包含 3 个 jsonArray。 这是 json 响应。这是一个 GET 请求并通过 Volley 请求发送。 [ "0x9000",
在此 Json 响应中,如何使用 Java 访问“smallImageUrls”并获取图像 url? 似乎“smallImageUrls”是“匹配”jsonarray 中的一个数组。如果我错了,请有人
这个问题在这里已经有了答案: Convert string to JSON array (10 个答案) 关闭 5 年前。 如何正确地将此字符串转换为 jsonArray? { "myArr
是否可以从较新的 EJB JSON 库转换为较旧的 org.json 库,而无需依赖 org.json.simple 或 GSON? 在下面的例子中,“buttons”是一个填充的 JsonArr
如何从jsonarray获取jsonarray? 我试过的代码。 for(loop) List stringList = new ArrayLis
我目前是 Json 新手,遇到了一个问题。我搜索了很多但找不到答案! 我从 json url 获取名称列表。名称可以在此 json 文件中重复,但我只想将它们的一条记录保留到我称为“arr”的新数组中
我是一名优秀的程序员,十分优秀!