简体   繁体   中英

Converting Arraylist<Arraylist<Integer>> to json

I am quite new to java and android so please keep that in mind.

I have an Arraylist of an Arraylist of integers. These integers are data from the GPS like Latitude, longtitude, speed at that moment and distance, added to an arraylist in OnLocationChanged and all these arraylists are then added to another arraylist.(sort of like a matrix or table)

example : [[timestamp,lat,long,distance_from_start,speed],[...],...] (all are integers)

I want to convert this Arraylist of arraylists so i can save it on the internal storage of my app for use in other activities ( like statistics of this data) and to upload it to a server. I have searched around quite a bit and found that converting an arraylist to json allows this and also makes it easier to create an SQL file of this data. The conversion of the arraylist to json seems easy enough but i can't find any examples of converting an arraylist of arraylists to json. So i dont know if the arraylists in the arraylist are converted to jsonarrays or whatever or if they will be usable and readable from the json file at all. If this is not possible, are there any other alternative ways of doing this?

Thanks a lot!

You can use Gson from Google.

Your main functions are: toJson and fromJson .

From the javadoc:

toJson(Object src)

This method serializes the specified object into its equivalent Json representation.

fromJson(String json, Type typeOfT)

This method deserializes the specified Json into an object of the specified type.

For example:

(Serialization)

Gson gson = new Gson();
gson.toJson(1);            ==> prints 1
gson.toJson("abcd");       ==> prints "abcd"
gson.toJson(new Long(10)); ==> prints 10
int[] values = { 1 };
gson.toJson(values);       ==> prints [1]

(Deserialization)

int one = gson.fromJson("1", int.class);
Integer one = gson.fromJson("1", Integer.class);
Long one = gson.fromJson("1", Long.class);
Boolean false = gson.fromJson("false", Boolean.class);
String str = gson.fromJson("\"abc\"", String.class);
String anotherStr = gson.fromJson("[\"abc\"]", String.class);

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

List of Lists of Integers

List<List<Integer >> list = new ArrayList<List<Integer>>();
List<Integer> list1=new ArrayList();
list1.add(100);
list1.add(200);
list.add(list1);
List<Integer> list2=new ArrayList();
list2.add(700);
list2.add(800);
list.add(list2);
Gson gson = new Gson()
String json = gson.toJson(list);
System.out.println(json);

Use org.json.JsonArray library.

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;

public class Test {
    public static void main(String[] args) {
        List<List<Integer >> list= new ArrayList<List<Integer>>();
        List<Integer> list1=new ArrayList();
        list1.add(10);
        list1.add(20);
        list.add(list1);
        List<Integer> list2=new ArrayList();
        list2.add(60);
        list2.add(70);
        list.add(list2);
        JSONArray jsonArray= new JSONArray(list);
        System.out.println(jsonArray);  
    }


}

output:

[[10,20],[60,70]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM