简体   繁体   English

转换数组列表 <Arraylist<Integer> &gt;至json

[英]Converting Arraylist<Arraylist<Integer>> to json

I am quite new to java and android so please keep that in mind. 我对Java和android很陌生,因此请记住这一点。

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) 这些整数是来自GPS的数据,例如纬度,经度,当时的速度和距离,并添加到OnLocationChanged中的一个数组列表中,然后所有这些数组列表都添加到另一个数组列表中(类似于矩阵或表格)。

example : [[timestamp,lat,long,distance_from_start,speed],[...],...] (all are integers) 例如 :[[timestamp,lat,long,distance_from_start,speed],[...],...](均为整数)

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. 我想转换此arraylists的Arraylist,以便将其保存在应用程序的内部存储中,以用于其他活动(例如此数据的统计信息),并将其上传到服务器。 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. 我进行了很多搜索,发现将arraylist转换为json可以实现此目的,并且还可以更轻松地创建此数据的SQL文件。 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. 将arraylist转换为json似乎很容易,但我找不到将arraylists的arraylist转换为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. 所以我不知道是否将arraylist中的arraylists转换为jsonarrays或其他内容,或者它们是否可以从json文件中使用和读取。 If this is not possible, are there any other alternative ways of doing this? 如果这不可能,那么还有其他替代方法吗?

Thanks a lot! 非常感谢!

You can use Gson from Google. 您可以使用Google的Gson

Your main functions are: toJson and fromJson . 您的主要功能是: toJsonfromJson

From the javadoc: 从javadoc:

toJson(Object src) toJson(对象src)

This method serializes the specified object into its equivalent Json representation. 此方法将指定的对象序列化为其等效的Json表示形式。

fromJson(String json, Type typeOfT) fromJson(字符串json,类型typeOfT)

This method deserializes the specified Json into an object of the specified type. 此方法将指定的Json反序列化为指定类型的对象。

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. 使用org.json.JsonArray库。

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]] [[10,20],[60,70]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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