Gson turn an array of data objects into json - Android

前端 未结 2 1121
旧巷少年郎
旧巷少年郎 2020-12-02 18:31

Currently I am working on a native android app with webView front end.

I have something like:

public class dataObject
{
  int a;
  String b;
}


        
相关标签:
2条回答
  • 2020-12-02 18:56

    My version of gson list deserialization using a helper class:

    public List<E> getList(Class<E> type, JSONArray json) throws Exception {
        Gson gsonB = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    
        return gsonB.fromJson(json.toString(), new JsonListHelper<E>(type));
    }
    
    
    
    public class JsonListHelper<T> implements ParameterizedType {
    
      private Class<?> wrapped;
    
      public JsonListHelper(Class<T> wrapped) {
        this.wrapped = wrapped;
      }
    
      public Type[] getActualTypeArguments() {
        return new Type[] {wrapped};
      }
    
      public Type getRawType() {
        return List.class;
      }
    
      public Type getOwnerType() {
        return null;
      }
    
    }
    

    Usage

    List<Object> objects = getList(Object.class, myJsonArray);
    
    0 讨论(0)
  • 2020-12-02 18:57

    Here's a comprehensive example on how to use Gson with a list of objects. This should demonstrate exactly how to convert to/from Json, how to reference lists, etc.

    Test.java:

    import com.google.gson.Gson;
    import java.util.List;
    import java.util.ArrayList;
    import com.google.gson.reflect.TypeToken;
    import java.lang.reflect.Type;
    
    
    public class Test {
    
      public static void main (String[] args) {
    
        // Initialize a list of type DataObject
        List<DataObject> objList = new ArrayList<DataObject>();
        objList.add(new DataObject(0, "zero"));
        objList.add(new DataObject(1, "one"));
        objList.add(new DataObject(2, "two"));
    
        // Convert the object to a JSON string
        String json = new Gson().toJson(objList);
        System.out.println(json);
    
        // Now convert the JSON string back to your java object
        Type type = new TypeToken<List<DataObject>>(){}.getType();
        List<DataObject> inpList = new Gson().fromJson(json, type);
        for (int i=0;i<inpList.size();i++) {
          DataObject x = inpList.get(i);
          System.out.println(x);
        }
    
      }
    
    
      private static class DataObject {
        private int a;
        private String b;
    
        public DataObject(int a, String b) {
          this.a = a;
          this.b = b;
        }
    
        public String toString() {
          return "a = " +a+ ", b = " +b;
        }
      }
    
    }
    

    To compile it:

    javac -cp "gson-2.1.jar:." Test.java
    

    And finally to run it:

    java -cp "gson-2.1.jar:." Test
    

    Note that if you're using Windows, you'll have to switch : with ; in the previous two commands.

    After you run it, you should see the following output:

    [{"a":0,"b":"zero"},{"a":1,"b":"one"},{"a":2,"b":"two"}]
    a = 0, b = zero
    a = 1, b = one
    a = 2, b = two
    

    Keep in mind that this is only a command line program to demonstrate how it works, but the same principles apply within the Android environment (referencing jar libs, etc.)

    0 讨论(0)
提交回复
热议问题