如何将JSON数据拆分为多个对象 [英] How to split JSON data to multiple objects

查看:248
本文介绍了如何将JSON数据拆分为多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自应用程序的JSON数据,格式如下:

I have JSON data coming from an application in the below format:

[{ "name":"First",
   "Custnumber":"123SC"
 },
 {"name":"Second",
   "Custnumber":"67BC"
 },
 {"name":"Third",
   "Custnumber":"99ABC"
 },
 {"name":"Fourth",
   "Custnumber":"123SC"
 }]



现在,我必须根据Custnumber将其拆分为:


Now, I have to split this based on Custnumber with which should be like this:

[{ "name":"First",
   "Custnumber":"123SC"
 },
 {"name":"Fourth",
   "Custnumber":"123SC"
 }]




[{"name":"Second",
  "Custnumber":"67BC"
}]




[{"name":"Third",
  "Custnumber":"99ABC"
}]





我的尝试:



请帮我解决这个问题。我被困在这里2天。



提前谢谢。



What I have tried:

Please help me solving this. I am stuck here from 2 days.

Thanks in advance.

推荐答案

假设分组将在客户端进行?以下是一个示例: Group json - JSFiddle [ ^ ]



分组后JSON将如下所示

Assuming the grouping will happen on the client side? Here is an example : Group json - JSFiddle[^]

The JSON will look like below after the grouping
{
   "123SC":[
      {
         "name":"First",
         "Custnumber":"123SC"
      },
      {
         "name":"Fourth",
         "Custnumber":"123SC"
      }
   ],
   "67BC":[
      {
         "name":"Second",
         "Custnumber":"67BC"
      }
   ],
   "99ABC":[
      {
         "name":"Third",
         "Custnumber":"99ABC"
      }
   ]
}





这里通过循环对象输出





Here the output by looping the object

Custnumber 123SC has 2 customers :
---->1. First.
---->2. Fourth.
Custnumber 67BC has 1 customers :
---->1. Second.
Custnumber 99ABC has 1 customers :
---->1. Third. 





参考:

javascript - 按值对JSON进行分组 - Stack Overflow [ ^ ]


只需将json反序列化为对象,从那些对象中选择想要的数据然后重新序列化。



Just deserialise the json to objects, select the data from want from those objects then re-serialise.

[DataContract]
public class Data
{
    [DataMember(Name = "name")]
    public string Name { get; set; }
    [DataMember]
    public string Custnumber { get; set; }
}







string json = "[{\"name\":\"First\", \"Custnumber\":\"123SC\" }, { \"name\":\"Second\", \"Custnumber\":\"67BC\" }, { \"name\":\"Third\", \"Custnumber\":\"99ABC\" }, { \"name\":\"Fourth\", \"Custnumber\":\"123SC\" }]";

List<Data> data = null;

// System.Runtime.Serialization.Json.DataContractJsonSerializer is just one way to
// serialise JSON, use whatever way you want to do this bit
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Data>));
data = ser.ReadObject(ms) as List<Data>;
ms.Close();

// get just the person you're interested in

data = data.Where(d => d.Custnumber == "123SC").ToList();

// serialize back to JSON

ms = new MemoryStream();

ser.WriteObject(ms, data);
ms.Position = 0;
json = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();





你需要为JSON序列化器使用这些,但正如我所说的那样使用你通常使用。





you'll need these usings for that JSON serialiser, but as I said use whatever you normally use.

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;


这篇关于如何将JSON数据拆分为多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆