java解析json数组:需要添加一个jar包,自己搜索即可
import
org.json.JSONArray;
import
org.json.JSONException;
import
org.json.JSONObject;
public
static void main(String[] args)
throws JSONException {
String sJson =
"[{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'3','spsl':'4'}]";
JSONArray jsonArray =
new JSONArray(sJson);
int
iSize = jsonArray.length();
System.out.println("Size:"
+ iSize);
for
(int
i = 0; i < iSize; i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
System.out.println("["
+ i + "]gwcxxid="
+ jsonObj.get("gwcxxid"));
System.out.println("["
+ i + "]spsl="
+ jsonObj.get("spsl"));
System.out.println();
}
}
|
本文提供了一个使用Java解析JSON数组的方法,包括导入必要的库、创建JSONArray实例、获取数组长度及遍历数组输出元素。
916

被折叠的 条评论
为什么被折叠?



