从JSON到2D数组 [英] from JSON to 2D array
本文介绍了从JSON到2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有json,我想将它们存储为2D数组.
I have json and i want to store them 2D array.
这是我的json
[
{
"IsStudent":true,
"Name":"Ali",
"age":16,
"ID":1
},
{
"IsStudent":false,
"Name":"Emad",
"age":17,
"ID":2
}
]
所以我想将所有信息存储到2D数组中:
so I want to store all the information to 2D array :
array [0] [0] = true
array[0][0] = true
array [0] [1] =阿里
array[0][1] = Ali
array [0] [2] = 16
array[0][2] = 16
array [0] [3] = 1
array[0][3] = 1
以此类推.
我尝试使用split和join来获取这些值,但这对我不起作用
I tried alot to get those values by using split and join but it doesn't work for me
JSONArray jarr2 = new JSONArray("my json is here");
String[] resultingArray = jarr.join("\":").split(",\"");
System.out.println(resultingArray[3]);
推荐答案
阅读json文件示例:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class Test {
public static void main(String[] args) {
FileReader reader;
try {
reader = new FileReader("/test.json");
JsonParser jsonParser = new JsonParser();
JsonArray array = (JsonArray) jsonParser.parse(reader);
searchJsonElemnet(array);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
private static void searchJsonElemnet(JsonArray jsonArray){
String[][] matrix = new String[2][4];
int i =0;
int j = 0;
for (JsonElement jsonElement : jsonArray) {
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
matrix[i][j] = entry.getValue().toString();
j++;
}
i++;
j = 0;
}
for (String[] row : matrix)
{
for (String value : row)
{
System.out.println(value);
}
}
}
}
输出:
是 阿里" 16 1个 错误的 "Emad" 17 2
true "Ali" 16 1 false "Emad" 17 2
这篇关于从JSON到2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文