[英]JSON Array to ArrayList<List<String>>
I have an JSON Array that looks like this 我有一个看起来像这样的JSON数组
[["ONE","CAT",0],["TWO","DOG",0]]
And I want to make it into an ArrayList<List<String>>
, I am trying to loop though it but can't get it to work. 而且我想使其成为
ArrayList<List<String>>
,尽管它试图循环但无法正常工作。
I have tried 我努力了
for (ArrayList arrayList: jsonArray) {
for (Object array : arrayList) {
}
}
But then I got a compilation error. 但是然后我得到了一个编译错误。 I'm not able to loop through an Array of JSON Objects.
我无法遍历JSON对象数组。
You can try this way - 您可以尝试这种方式-
try {
ArrayList<Object> _arrList = new ArrayList<Object>();
JSONArray _jArryMain = new JSONArray("YOUR_JSON_STRING");
if (_jArryMain.length()>0) {
for (int i = 0; i < _jArryMain.length(); i++) {
JSONArray _jArraySub = _jArryMain.getJSONArray(i);
_arrList.add(_jArraySub);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
First, Java does not like mixed types. 首先,Java不喜欢混合类型。 If you are sure you can use
List<List<String>>
then keep reading. 如果您确定可以使用
List<List<String>>
请继续阅读。
I recommend using the Jackson Library and ObjectMapper#readTree
. 我建议使用Jackson库和
ObjectMapper#readTree
。
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("JSON CONTENT");
List<List<String>> convertedList = new ArrayList<List<String>>();
for (JsonNode arrNode : node) {
List<String> currList = new ArrayList<String>();
convertedList.add(currList);
for (JsonNode dataNode : arrNode) {
currList.add(dataNode.asText());
}
}
System.out.println(convertedList);
I guess this is what your looking for : 我想这就是您要寻找的:
try {
ArrayList<ArrayList<Object>> mainarraylist = new ArrayList<ArrayList<Object>>();
JSONArray jsonarray = new JSONArray(yourstringjson);
if (jsonarray.length()>0) {
for (int i = 0; i < jsonarray.length(); i++) {
ArrayList<String> subarray;
JSONArray jsonsubarray = jsonarray.getJSONArray(i);
for (int i = 0; i < jsonsubarray.length(); i++) {
subarray = new ArrayList<String>;
jsonsubarray.add(jsonsubarray.get(i));
}
mainarraylist.add(jsonsubarray);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This will give you the result: mainarraylist.get(0).get(0) equals ONE
这将为您提供结果:
mainarraylist.get(0).get(0) equals ONE
FYI: The code is not compiled, so there might some errors. 仅供参考:该代码未编译,因此可能会出现一些错误。 This is an abstract for your solution.
这是您的解决方案的摘要。
you can use this code to solve your problems 您可以使用此代码解决您的问题
String jsonValue = "[[\"ONE\",\"CAT\",0],[\"TWO\",\"DOG\",0]]";
try{
JSONArray array = (JSONArray) new JSONTokener(jsonValue).nextValue();
ArrayList<JSONArray> arrayList = new ArrayList<>();
arrayList.add(array);
for (int i=0;i<arrayList.get(arrayList.size()-1).length();i++) {
try {
JSONArray arr = arrayList.get(arrayList.size()-1).getJSONArray(i);
for (int j = 0; j < arr.length(); j++) {
System.out.println("Value = " + arr.get(j));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}catch (JSONException e){
}
1. try {
String abc = "[[\"ONE\",\"CAT\",0],[\"TWO\",\"DOG\",0]]";
JSONArray jsonarray = new JSONArray(abc);
if (jsonarray.length()>0) {
for (int i = 0; i < jsonarray.length(); i++) {
JSONArray jsonsubarray = jsonarray.getJSONArray(i);
for (int j = 0; j < jsonsubarray.length(); j++) {
Log.d("Value---->",""+jsonsubarray.get(j));
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
To parse a json array to a list of list i have created the following method. 要将json数组解析为列表列表,我创建了以下方法。
public static List<List<String>> convertJsonToListofList(String json) throws Exception{
JSONArray jArray=new JSONArray(json);
List<List<String>> list = new ArrayList<List<String>>();
int length = jArray.length();
for(int i=0; i<length;i++) {
JSONArray array;
List<String> innerlist = new ArrayList<String>();
String innerJsonArrayString = jArray.get(i).toString();
JSONArray innerJsonArray = new JSONArray(innerJsonArrayString);
int innerLength = innerJsonArray.length();
for(int j=0;j<innerLength;j++){
String str = innerJsonArray.getString(j);
innerlist.add(str);
}
list.add(innerlist);
}
return list;
}
You have to call this method in this way: 您必须以这种方式调用此方法:
try {
List<List<String>> listoflist = convertJsonToListofList("[[\"ONE\",\"CAT\",0],[\"TWO\",\"DOG\",0]]");
System.out.println(listoflist);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The output I have obtained is this one 我获得的输出是这个
07-09 13:00:42.268: I/System.out(19986): [[ONE, CAT, 0], [TWO, DOG, 0]]
I hope this is what you are looking for :) 我希望这是您正在寻找的:)
Another Jackson approach. 杰克逊的另一种方法。
ObjectMapper mapper = new ObjectMapper();
String json = "[[\"ONE\",\"CAT\",0],[\"TWO\",\"DOG\",0]]";
List<List<String>> output = new ArrayList<List<String>>();
JsonNode root = mapper.readTree(json);
for (JsonNode child : root) {
output.add(mapper.readValue(child.traverse(), new TypeReference<ArrayList<String>>(){}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.