[英]Parse JSON number array using Gson
This is the JSON string I want to parse with Gson. 这是我要与Gson解析的JSON字符串。 I would like to quick and simple to parse it.
我想快速简单地解析它。
{
"values": [
[
1,
1,
0,
0,
0,
0,
11,
0.09090909090909091
],
[
[
0,
0,
0,
0,
0,
1,
0
],
[
0,
0,
0,
0,
0,
1,
0
],
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
11,
0
],
[
0,
0,
0,
0,
0,
0.09090909090909091,
0
]
]
]
}
but I don't know how to write the parsing code with Gson. 但是我不知道如何用Gson编写解析代码。 Best result is if I can extract List from the parsed object.
最好的结果是我是否可以从解析的对象中提取List。
Is any possible that i Write a class like that. 我有可能写这样的课程吗?
class Value
{
@SerializedName("0")
List<Float> value1;
@SerializedName("1")
List<Integer> value2;
...........
}
Thanks for everyone to help me. 谢谢大家的帮助。
This is the fastest way I know to get the info you need in the most usable format for you. 我知道这是最快的方式,它以最实用的格式为您获取所需的信息。
Your JSON is made of a list that contains list1 - a list of double - and list2, a list of list of double. 您的JSON由包含list1(一个双精度列表)和list2(一个双精度列表列表)的列表组成。 Assuming that you want to parse exactly the example you provided with, this is a self contained class that you can use.
假设您想完全解析所提供的示例,则这是一个可以使用的自包含类。
If you need mode details on how it works, feel free to ask. 如果您需要有关其工作方式的详细信息,请随时询问。
package stackoverflow.questions;
import java.lang.reflect.Type;
import java.util.List;
import stackoverflow.questions.Test.Wrapper;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
public class Q20118749 {
/**
* @param args
*/
public static void main(String[] args) {
String json = "{\"values\":[[1,1,0,0,0,0,11,0.09090909090909091],[[0,0,0,0,0,1,0],[0,0,0,0,0,1,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,11,0],[0,0,0,0,0,0.09090909090909091,0]]]}";
JsonElement je = new JsonParser().parse(json);
JsonArray list = je.getAsJsonObject().get("values").getAsJsonArray(); // to get rid of the value part
Type listType1 = new TypeToken<List<Double>>() {}.getType();
Type listType2 = new TypeToken<List<List<Double>>>() {}.getType();
Gson g = new Gson();
List<Double> listOfDouble = g.fromJson(list.get(0), listType1);
List<List<Double>> listOfListOfDouble = g.fromJson(list.get(1), listType2);
System.out.println(listOfDouble);
System.out.println(listOfListOfDouble);
}
}
This is my execution: 这是我的处决:
[1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 11.0, 0.09090909090909091]
[[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 11.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.09090909090909091, 0.0]]
If your data changes, of course, you have to change this code according. 当然,如果您的数据发生更改,则必须根据此代码进行更改。
Use this code. 使用此代码。
public class MyObject{
@SerializedName("values")
private Values[] mValues;
public Value[] getLoc() {
return mvalues;
}
}
public class Values{
@SerializedName("0")
private Zero[] mZero;
@SerializedName("1")
private One[] mOne;
public Zero[] getZero() {
return mZero;
}
public Value[] getOne() {
return mOne;
}
}
public class Zero{
@SerializedName("0")
private zint zero;
public int getZeroValue() {
return zero;
.........
}
}
like this you have to write 像这样你必须写
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.