简体   繁体   English

多个JSON对象数组

[英]Multiple JSON Object Arrays

How would i go about making multiple JSON arrays from 1 JSON array? 我将如何从1个JSON数组制作多个JSON数组?

Basically i have an array filled with JSON objects, but i would like to create multiple arrays with each array being filled with specific JSON objects from the initial array depending on a certain property like ID that every JSON object posesses. 基本上我有一个用JSON对象填充的数组,但是我想创建多个数组,每个数组都由初始数组中的特定JSON对象填充,具体取决于每个JSON对象所构成的ID等特定属性。

I work in Unity3D with C#. 我使用C#在Unity3D中工作。

Anyone any ideas? 有任何想法吗?

[EDIT] [编辑]

this is 1 object: 这是1个对象:

{"ID":175355,"Datetime":1523612270,"Module":"Krol 42","Latitude":52.08618,"Longitude":5.11126166666667,"Speed":0} {“ ID”:175355,“日期时间”:1523612270,“模块”:“ Krol 42”,“纬度”:52.08618,“经度”:5.11126166666667,“速度”:0}

There are 50 different object with individual IDs in the array but every ID has a 100 instances of it with different lat/lon coordinates 数组中有50个具有单独ID的不同对象,但是每个ID都有100个具有不同纬度/经度坐标的实例

so what i would like is to have an array filled with 50 arrays, so each unique ID has its own array with all the 100 different instances of it in it. 所以我想让一个数组充满50个数组,所以每个唯一ID都有自己的数组,其中包含所有100个不同的实例。

if this makes any sense, i dont know if im explaining it clearly enough, sorry about that. 如果这有任何意义,我不知道即时通讯是否足够清楚地解释,对此感到抱歉。

Generate a class to hold the JSON data: 生成一个类来保存JSON数据:

public class ModuleInfo
{
    public int ID { get; set; }
    public int Datetime { get; set; }
    public string Module { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double Speed { get; set; }
}

Then deserialize the JSON array: 然后反序列化JSON数组:

var moduleInfo = JsonConvert.DeserializeObject<List<ModuleInfo>>(jsonString);

Then group by Id using Linq: 然后使用Linq按ID分组:

var groupedModuleInfo = moduleInfo.GroupBy(m => m.ID).ToArray();

Then you can serialize that array again: 然后,您可以再次序列化该数组:

var groupedJson = JsonConvert.SerializeObject(groupedModuleInfo);

This will yield an array of arrays, where each inner array contains all records for one ID: 这将产生一个数组数组,其中每个内部数组都包含一个ID的所有记录:

[[{
        "ID": 60034,
        "Datetime": 1519029071,
        "Module": "Krol 42",
        "Latitude": 51.8423083333333,
        "Longitude": 4.57711,
        "Speed": 0.59264
    }
], [{
        "ID": 58961,
        "Datetime": 1519025476,
        "Module": "Krol 42",
        "Latitude": 51.8422666666667,
        "Longitude": 4.576865,
        "Speed": 0.59264
    }
]]

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

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