如何在 C# 中获取 Json 数组?

5
我是一名有用的助手,可以为您翻译文本。

我有一个像这样的Json字符串,我想在C#数组中加载它。当我尝试做到这一点时,我会得到异常。

我的字符串:

 {
"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

先尝试使用较小的样本,然后逐步扩大。沿途的一些提示是,您要转换的对象应与 JSON 具有相同的结构,在您的示例代码中它并没有这样。 - Tomas Jansson
在您的JSON中,数组被称为“customerInformation”,但在您的容器类中,它被称为“attributes”。尝试在两个地方使用相同的名称。 - alun
2个回答

9
您生成对象的方式不正确,应该像这样:
public class StudyInfo
{
    public string Modality { get; set; }
    public string StudyName { get; set; }
    public string ModalityId { get; set; }
    public string StudyID { get; set; }
    public int visitid { get; set; }
    public string billingId { get; set; }
    public string RegDate { get; set; }
    public string uploaded { get; set; }
    public string groupid { get; set; }
}

public class CustomerInformation
{
    public string customerId { get; set; }
    public string CustomerName { get; set; }
    public int Age { get; set; }
    public string Gender { get; set; }
    public List<StudyInfo> StudyInfo { get; set; }
}

public class RootObject
{
    public List<CustomerInformation> customerInformation { get; set; }
}

顺便提一下,你可以试试json2charp这个工具,对于这种东西,它非常棒。


1

用户2552410!

也许你需要改变你的类结构。你可以使用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);
        }
    }
}

但是字符串应该以“customerInformation”开头:您直接添加了客户ID。 - Aravind
是的,我认为最好创建两个表。一个是“客户信息”表,另一个是“访问详情”表。其中,“客户信息”表将包含“访问详情”的列表。 - IluhaPuts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接