JSON parsing to Java - Android application

后端 未结 5 1451
情话喂你
情话喂你 2020-11-30 14:40

I need help with parsing json string in Java Android Appl.

Text of JSON file:

{\"data\":{\"columns\":[\"location_id\",\"name\",\"description\",\"lati         


        
5条回答
  •  渐次进展
    2020-11-30 15:32

    You need to use the GSON lib http://code.google.com/p/google-gson/

    Object Examples

    class BagOfPrimitives {
      private int value1 = 1;
      private String value2 = "abc";
      private transient int value3 = 3;
      BagOfPrimitives() {
        // no-args constructor
      }
    }
    

    (Serialization)

    BagOfPrimitives obj = new BagOfPrimitives();
    Gson gson = new Gson();
    String json = gson.toJson(obj);  
    ==> json is {"value1":1,"value2":"abc"}
    

    Note that you can not serialize objects with circular references since that will result in infinite recursion.

    (Deserialization)

    BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   
    ==> obj2 is just like obj
    

提交回复
热议问题