1

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.

3
  • 2
    Good to know that you wantto parse, but what data do you want to fetch? Commented Nov 21, 2013 at 10:46
  • And also, what have you tried so far ? Commented Nov 21, 2013 at 10:46
  • I want to get all infomation parse Commented Nov 21, 2013 at 10:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I update my question,Thanks for answer me.But I really want to write a class using the class.Thanks
What exactly does not satisfy you?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.