如何在c#中读取基于索引的json并将其转换为对象

本文关键字:json 对象 转换 索引 读取 于索引 | 更新日期: 2023-09-27 18:28:23

它是使用MVC 5应用程序的web服务。Web服务具有返回以下格式字符串的JSON数据的方法。

 [WebMethod]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public string GetCompanyData()
 {
        string jsonData = "[{'"1'":'"Message-Type'"},{'"28'":'"KEM'",'"3'":'"COMPANY1'",'"6'":'"218'",'"21'":'"6.8'",'"14'":'"33543'",'"16'":'"7188572.3'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY2'",'"6'":'"224.5'",'"21'":'"4.5'",'"14'":'"19058'",'"16'":'"4246936'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY3'",'"6'":'"79.9'",'"21'":'"3.4'",'"14'":'"81418'",'"16'":'"6320237.5'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY4'",'"6'":'"87'",'"21'":'"2.5'",'"14'":'"42277'",'"16'":'"3654459'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY5'",'"6'":'"103'",'"21'":'"2.3'",'"14'":'"1735'",'"16'":'"177450.4'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY6'",'"6'":'"108.1'",'"21'":'"2.1'",'"14'":'"269165'",'"16'":'"29039148.4'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY7'",'"6'":'"95.9'",'"21'":'"1.2'",'"14'":'"313'",'"16'":'"29479.7'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY8'",'"6'":'"51.1'",'"21'":'"1'",'"14'":'"117208'",'"16'":'"5954460.6'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY9'",'"6'":'"73.6'",'"21'":'"0.9'",'"14'":'"161593'",'"16'":'"11856197.6'"},";
        jsonData +="{'"28'":'"KEM'",'"3'":'"COMPANY10'",'"6'":'"40.1'",'"21'":'"0.55'",'"14'":'"220241'",'"16'":'"8782243.3'"}]";
        return jsonData;
  }

正在尝试将其转换为对象列表。

 In controller :
  JavaScriptSerializer json_serializer = new JavaScriptSerializer();
  var companyDataList = json_serializer.Deserialize<List<object>>(svc.GetCompanyData())

这很好用。但JSON中有一个迭代,需要对自定义对象执行开箱操作。由于键是基于整数的,因此无法从JSON中读取数据。但是生成JSON的关键是int,因此无法读取特定的数据。如何读取此类JSON数据。编辑:试用Newtonsoft

object[] objectArray = JsonConvert.DeserializeObject<object[]>(JsonConvert.SerializeObject(companyDataList ));

但数据的底层列表正在产生类似于(第一次)的即时窗口:

objIndex
{
  "28": "KEM",
  "3": "COMPANY1",
  "6": "218",
  "21": "6.8",
  "14": "33543",
  "16": "7188572.3"
}
    base: {
  "28": "KEM",
  "3": "COMPANY1",
  "6": "218",
  "21": "6.8",
  "14": "33543",
  "16": "7188572.3"
}
    Type: Object

答案:

添加了以下类以扩展:

 public static class Extensions
    {
        public static T ToObject<T>(this IDictionary<string, object> source, Dictionary<string, string> sourceDictionary)
          where T : class, new()
        {
            T someObject = new T();
            Type someObjectType = someObject.GetType();
            foreach (KeyValuePair<string, object> item in source)
            {
                if (sourceDictionary.Keys.Contains(item.Key) && (someObjectType.GetProperty(sourceDictionary[item.Key])!=null))
                    someObjectType.GetProperty(sourceDictionary[item.Key]).SetValue(someObject, item.Value, null);
            }
            return someObject;
        }
        public static IDictionary<string, object> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
        {
            return source.GetType().GetProperties(bindingAttr).ToDictionary
            (
                propInfo => propInfo.Name,
                propInfo => propInfo.GetValue(source, null)
            );
        }
    } 

类内方法将其用作:

var tempObjArray = json_serializer.Deserialize<object[]>(svc.GetTopByVolume());
        List<Symbol> topByVolumeList = new List<Symbol>();
        foreach (object tmpObject in tempObjArray)
        {
            Dictionary<string, object> diTopByVolumes = (Dictionary<string, object>)tmpObject;
            Symbol someObject = diTopByVolumes.ToObject<Symbol>(StaticDictionary.TopByVolumeDictionary);
            topByVolumeList.Add(someObject);
        }

也在Global.asax中指定的应用程序启动事件中添加:

     public static Dictionary<string, string> TopByVolumeDictionary = new Dictionary<string, string>();
 TopByVolumeDictionary.Add("3", "SYMBOLID");
            TopByVolumeDictionary.Add("6", "Property1");
            TopByVolumeDictionary.Add("14", "Property2");
            TopByVolumeDictionary.Add("16", "Property3");
            TopByVolumeDictionary.Add("21", "Property4");

如何在c#中读取基于索引的json并将其转换为对象

为了稍微扩展一下我的评论,

您最好构建一个简单的类或对象,反映您的JSON应该是什么样子,然后制作一个可以转换为JSON字符串的List<companyObject>

一个可能不能反映你想要的结构的快速例子是:

A级:

public class Company
{
    public Dictionary<string, string> companyObject = 
        new Dictionary<string, string>();
    public Dictionary<string, string> 
        Add(string twentyEight, string three, string six,
        string twentOne, string fourteen, string sixteen)
    {
        companyObject.Add("28", twentyEight);
        companyObject.Add("3", three);
        companyObject.Add("6", six);
        companyObject.Add("21", twentOne);
        companyObject.Add("14", fourteen);
        companyObject.Add("16", sixteen);
        return companyObject;
    }
}

然后是一个小代码(不优雅)

List<Company> companyList = new List<Company>();
Company c = new Company();
c.Add("KEM", "COMPANY1", "218", "6.8", "33543", "7188572.3");
companyList.Add(c);
string newJson = Newtonsoft.Json.JsonConvert.SerializeObject(companyList);

会创建一个JSON,看起来像:

[{"companyObject":{"28":"KEM","3":"COMPANY1","6":"218","21":"6.8",
"14":"33543","16":"7188572.3"}}]

两个项目的列表如下所示:

[{"companyObject":{"28":"KEM","3":"COMPANY1","6":"218","21":"6.8",
"14":"33543","16":"7188572.3"}},
{"companyObject":{"28":"KEM","3":"COMPANY2","6":"219","21":"7.2",
"14":"35200","16":"7188111.7"}}]

我会根据你真正想要的来调整这个类,但作为一个快速粗略的概念,我认为这是一个更好的方法。