JSON获取字符串数组
程序员文章站
2022-07-14 19:39:57
...
使用到的jar包:fastjson-1.2.2.jar
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
1.格式一:
{“aa”:["11","22","33"]}
public static void main(String[] args) {
String str = "{'aa':['11','22','33']}";
JSONObject jsonObject = JSONObject.parseObject(str);
JSONArray jsonArray = jsonObject.getJSONArray("aa");
System.out.println(jsonArray.get(0));//11
System.out.println(jsonArray.get(1));//22
System.out.println(jsonArray.get(2));//33
}
2.格式二:
"aaa" : [{"bb": "11","cc": "22","dd": "33"},{"bb": "44","cc": "55","dd": "66"}]
public static void main(String[] args) {
String str = "{'aa':[{'bb':'11','cc':'22','dd':'33'},{'bb':'44','cc':'55','dd':'66'}]}";
JSONObject jsonObject = JSONObject.parseObject(str);
JSONArray jsonArray = jsonObject.getJSONArray("aa");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
System.out.println("bb:" + object.getString("bb")
+ " cc:" + object.getString("cc")
+ " dd:" + object.getString("dd"));
}
}