Reputation: 1347
This is the JSON string I want to parse with Gson. 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. Best result is if I can extract List from the parsed object.
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.
Upvotes: 1
Views: 365
Reputation: 6934
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. 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.
Upvotes: 2
Reputation: 1996
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
Upvotes: 0