简体   繁体   English

如何解析android中的json响应

[英]how to parse a json response in android

can some parse the json encode response from php into java arraylist. 有些人可以将json编码响应从php解析为java arraylist。 My string is 我的字符串是

[{"Link_Id":"811"},{"Link_Id":"1389"},{"Link_Id":"6190"}]

what I tried is 我试过的是

static JSONObject jObj = null;
JSONArray link_Ids = null;
ArrayList<Integer> ids;
jObj = new JSONObject(a);
link_Ids = jObj.getJSONArray("Link_Id");
for (int i = 0; i < link_Ids.length(); i++) {  // **line 2**
     JSONObject childJSONObject = link_Ids.getJSONObject(i);
     ids.add(childJSONObject.getString(i));
}

can some one please correct me 有人可以纠正我

the string represents a json array so it should be like this: 字符串表示一个json数组,所以它应该是这样的:

link_Ids = new JSONArray(a);
for (int i = 0; i < link_Ids.length(); i++) {  
   JSONObject childJSONObject = link_Ids.getJSONObject(i);
   ids.add(childJSONObject.getString("Link_Id"));
}

You can use gson library for parsing JSON objects.It's Easy to use.Here is the Link for gson parsing 你可以使用gson库来解析JSON对象。它很容易使用。这里是gson解析的链接

Here is the Stack Overflow internal link 这是Stack Overflow 内部链接

For gson to work to need a .jar file.You can download the gson library from here 要让gson工作需要一个.jar文件。你可以从这里下载gson库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM