如何在C#中获取Json数组? [英] How to Get Json Array in C# ?

查看:116
本文介绍了如何在C#中获取Json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的Json字符串,我想将其加载到C#数组中.当我尝试执行此操作时,我得到了异常

I have a Json String Like this ,, and i wana load it in C# Array .. When i try to do this i am getting Exception

我的字符串:

 {
"customerInformation":
[
  {
     "customerId":"123",
     "CustomerName":"",
     "Age":39,
     "Gender":"Male",
     "StudyInfo":[
        {
           "Modality":"XRAY",
           "StudyName":"Test Name",
           "ModalityId":"1",
           "StudyID":"10923",
           "visitid":41549113,
           "billingId":"456",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"

        },
        {
           "Modality":"XRAY",
           "StudyName":"CT Test Name",
           "ModalityId":"1",
           "StudyID":"10924",
           "visitid":41549113,
           "billingId":"459",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"

        }
     ]
  },

  {
     "customerId":"928",
     "CustomerName":"",
     "Age":49,
     "Gender":"FeMale",
     "StudyInfo":[
        {
           "Modality":"XRAY",
           "StudyName":"Test Name",
           "ModalityId":"1",
           "StudyID":"10923",
           "visitid":41549113,
           "billingId":"456",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"
        },
        {
           "Modality":"XRAY",
           "StudyName":"CT Test Name",
           "ModalityId":"1",
           "StudyID":"10924",
           "visitid":41549113,
           "billingId":"459",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"
        }
     ]
  }

]

 }

我的代码:

public class Attributes
{


    public string[] customerId { get; set; }
    public string[] CustomerName { get; set; }
    public string[] Age { get; set; }
    public string[] Gender { get; set; }
    public string[] StudyInfo { get; set; }
    public string[] Modality { get; set; }
    public string[] StudyName { get; set; }
    public string[] ModalityId { get; set; }
    public string[] StudyID { get; set; }
    public string[] visitid { get; set; }
    public string[] billingId { get; set; }
    public string[] RegDate { get; set; }
    public string[] uploaded { get; set; }
}

public class DataJsonAttributeContainer
{
    public List<Attributes> attributes { get; set; }
}

 public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
       return deserializedProduct;
    }

   public void testing()
    {
  var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonString);

    }

这将返回Null"

我也尝试过

            JArray jArray = (JArray)JsonConvert.DeserializeObject(JsonStr);
            dynamic dynObj1 = jArray.OrderByDescending(x => x["customerId"]);

两个案例都失败了...如何加载此..我正在使用Newtonsoft.Json Dll

Both Cases got Failed... How to load this .. I am using Newtonsoft.Json Dll

推荐答案

user2552410!

user2552410!

也许您需要更改您的类结构.您可以使用List<>

Maybe you need to change your class structure. You can work with List<>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication2.TestService;
using Newtonsoft.Json;

namespace ConsoleApplication2
{
    public class Customer
    {
        public string customerId { get; set; }
        public string CustomerName { get; set; }
        public string Age { get; set; }
        public string Gender { get; set; }
        public StudyInfoType[] StudyInfo { get; set; }
        public string visited { get; set; }
        public string billingId { get; set; }
        public string RegDate { get; set; }
        public string uploaded { get; set; }
    }

    public class StudyInfoType
    {
           string Modality {get; set;}
           string StudyName {get; set;}
           string ModalityId {get; set;}
           string StudyID {get; set;}
           string visitid {get; set;}
           string billingId {get; set;}
           string RegDate {get; set;}
           string uploaded {get; set;}
           string groupid { get; set; }
    }


    class Program
    {
        static void Main()
        {
            var temp = CustomerInfo(@"[{ 'customerId':'123', 'CustomerName':'', 'Age':39,'Gender':'Male','StudyInfo':[{'Modality':'XRAY','StudyName':'Test Name','ModalityId':'1','StudyID':'10923','visitid':41549113,'billingId':'456','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'},{'Modality':'XRAY','StudyName':'CT Test Name','ModalityId':'1','StudyID':'10924','visitid':41549113,'billingId':'459','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'}]},{'customerId':'928','CustomerName':'','Age':49,'Gender':'FeMale','StudyInfo':[{'Modality':'XRAY','StudyName':'Test Name','ModalityId':'1','StudyID':'10923','visitid':41549113,'billingId':'456','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'},{ 'Modality':'XRAY','StudyName':'CT Test Name','ModalityId':'1','StudyID':'10924','visitid':41549113,'billingId':'459','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1' } ] } ]");
        }

        public static List<Customer> CustomerInfo(string json)
        {
            var n = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
            {
                ObjectCreationHandling = ObjectCreationHandling.Replace
            });
            return JsonConvert.DeserializeObject<List<Customer>>(json);
        }
    }
}

这篇关于如何在C#中获取Json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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