C# 反序列化C中值的JSON数组#

C# 反序列化C中值的JSON数组#,c#,arrays,json,C#,Arrays,Json,我在使用这个json脚本时遇到了一些困难: { "insured_agent_flag": "a", "id": "1", "agent": { "fullName": "John Travolta", "mobileNumberPdf": "+987654321", "mobileNumber": "", "identityCard": { "identityCardExpirationDate": null }, "se

我在使用这个json脚本时遇到了一些困难:

{
  "insured_agent_flag": "a",
  "id": "1",
  "agent": {
    "fullName": "John Travolta",
    "mobileNumberPdf": "+987654321",
    "mobileNumber": "",
    "identityCard": {
      "identityCardExpirationDate": null
    },
    "secondIdentityCard": {
      "identityCardExpirationDate": null
    },
    "notes": {},
    "sign": "ADVANCED"
  },
  "basicData": {
          "personType": "PERSON",
          "agreeWithCompleteAnalysis": false,
          "investmentInterest": false
  },
  "nonOfferedProducts": [
    "PROD_A",
    "PROD_B",
    "PROD_C"
  ]
}
我想从这个脚本中获取一些参数,并将其放入SQLServer表中。 为此,我使用并转换了一个C#脚本,该脚本由以下人员共享:

使用系统;
使用系统数据;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.SqlServer.Dts.Pipeline.Wrapper;
使用Microsoft.SqlServer.Dts.Runtime.Wrapper;
使用System.Web.Script.Serialization;
使用Microsoft.SqlServer.Dts.Pipeline;
命名空间SC_c7e2d8c3918d46a5a07a1b438ddc7642
{
公共类基本CDATA
{
公共字符串agreeWithCompleteAnalysis{get;set;}
其他系统{get;set;}中的公共字符串
公共字符串investmentInterest{get;set;}
}
公共类父对象
{
公共基本CDATA基本CDATA{get;set;}
公共int id{get;set;}
公共字符串\u代理\u标志{get;set;}
公共IEnumerable非提供产品{get;set;}
}
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
公共类ScriptMain:UserComponent
{
公共覆盖无效Input0\u进程InputRow(Input0Buffer行)
{
JavaScriptSerializer js=新的JavaScriptSerializer();
//为输入列指定一个变量,以便于引用。
BlobColumn combinedColumn=行。参数scon;
//从blob转换为字符串
string reviewConverted=System.Text.Encoding.ASCII.GetString(combinedColumn.GetBlobData(0,Convert.ToInt32(combinedColumn.Length));
//反序列化字符串
ParentObject obj=js.Deserialize(reviewConverted);
var rows=obj.NonOfferedProducts.ToList();
Row.agreeWithCompleteAnalysis=obj.BasicData.agreeWithCompleteAnalysis;
Row.inOtherSystem=obj.BasicData.inOtherSystem;
Row.investmentInterest=obj.BasicData.investmentInterest;
Row.projectionid=obj.id;
Row.insuredagentflag=对象被保险人\代理人\标志;
//行。未提供的产品=
}
}
}
对于“标准”对象,它可以正常工作,但数组“nonOfferedProducts”存在问题。编译后,我得到一个错误: “对象引用未设置为对象实例”

以下是我的问题: 1.我应该如何处理C#脚本中的“nonOfferedProducts”数组? 2.为什么我会犯上述错误? 3.不幸的是,json脚本可能会有一些错误,比如缺少大括号。我该怎么处理

谢谢大家!


非常感谢你的回答。根据你的评论,我将尝试给你更多的解释: 1.我在本文中添加的json脚本——它只是整个脚本的一小部分。在完整的脚本中有许多不同的参数。此外,我的C#代码应该扫描大约40000个json脚本(存储在SQLServer表中的一列中)。这些脚本具有相似的结构,但不尽相同。 所以我想到了C#分辨率,它将搜索我需要的参数。对于没有这些参数的json脚本,c#代码将在右边的输出列中放置null

以下是我的输出列: -同意完全分析 -在其他系统中 -投资利息 -投射体 -保险公司 -非报价产品

我明白,我的课程结构是错误的,我会改进的。 但我有一个疑问——是否有可能准备c代码结构,只处理我需要的这些参数

最后,我想把结果输入我的数据库。 例如,如果nonOfferedProducts属性将有3个值(不总是!),我想向数据库表发送3条记录(nonOfferedProducts列有3个不同的值,其余列有3个相同的值-agreeWithCompleteAnalysis,inOtherSystem等)

我希望现在情况会明朗。 非常感谢你的帮助


正如我在评论中所说,您的c#模型与JSON对象不匹配

如果模型是由各种嵌套对象组成的,以更好地反映实际的JSON,那么您会更幸运:

public class IdentityCard
{
    public DateTime? IdentityCardExpirationDate { get; set; }
}

public class Notes
{
    //No idea what should be in here... 
}
public class BasicData
{
    public string PersonType { get; set; }
    public bool AgreeWithCompleteAnalysis { get; set; }
    public bool InvestmentInterest { get; set; }
}

public class Agent
{
    public string FullName { get; set; }
    public string MobileNumberPdf { get; set; }
    public string MobileNumber { get; set; }
    public IdentityCard IdentityCard { get; set; }
    public IdentityCard SecondIdentityCard { get; set; }
    public Notes Notes { get; set; }
    public string Sign { get; set; }
}

//Note: THIS is the actual class that matches the JSON sample given.
public class ParentObject
{
    public string insured_agent_flag { get; set; }
    public int Id { get; set; }
    public Agent Agent { get; set; }
    public BasicData BasicData { get; set; }
    public IEnumerable<string> NonOfferedProducts { get; set; }
}
公共类标识卡
{
public DateTime?IdentityCardExpirationDate{get;set;}
}
公开课堂讲稿
{
//不知道这里应该有什么。。。
}
公共类基本CDATA
{
公共字符串PersonType{get;set;}
公共bool AgreeWithCompleteAnalysis{get;set;}
公共布尔投资利益{get;set;}
}
公共类代理
{
公共字符串全名{get;set;}
公共字符串MobileNumberPdf{get;set;}
公共字符串MobileNumber{get;set;}
公共标识卡标识卡{get;set;}
公共标识卡SecondIdentityCard{get;set;}
公共便笺{get;set;}
公共字符串符号{get;set;}
}
//注意:这是与给定JSON示例匹配的实际类。
公共类父对象
{
公共字符串\u代理\u标志{get;set;}
公共int Id{get;set;}
公共代理{get;set;}
公共基本CDATA基本CDATA{get;set;}
公共IEnumerable非提供产品{get;set;}
}
一旦模型正确,反序列化就可以在给定的示例中正常工作(我在单元测试中这样做了,但假设字符串与示例匹配,这应该可以)

//获取json
字符串json=@”
{
“被保险人、代理人、标志”:“a”,
“id”:“1”,
“代理人”:{
“全名”:“约翰·特拉沃尔塔”,
“mobileNumberPdf:”+987654321“,
“mobileNumber”“:”“”,
“识别卡”:{
“identityCardExpirationDate”:null
},
“第二标识卡”:{
“identityCardExpirationDate”:null
},
“”注释“”:{},
“”符号“”:“”高级“”
},
“基本CDATA”:{
“personType”:“PERSON”,
“同意完整分析”:false,
“投资权益”:false
},
“未提供的产品”:[
“PROD_A”,
“戳_
public class IdentityCard
{
    public DateTime? IdentityCardExpirationDate { get; set; }
}

public class Notes
{
    //No idea what should be in here... 
}
public class BasicData
{
    public string PersonType { get; set; }
    public bool AgreeWithCompleteAnalysis { get; set; }
    public bool InvestmentInterest { get; set; }
}

public class Agent
{
    public string FullName { get; set; }
    public string MobileNumberPdf { get; set; }
    public string MobileNumber { get; set; }
    public IdentityCard IdentityCard { get; set; }
    public IdentityCard SecondIdentityCard { get; set; }
    public Notes Notes { get; set; }
    public string Sign { get; set; }
}

//Note: THIS is the actual class that matches the JSON sample given.
public class ParentObject
{
    public string insured_agent_flag { get; set; }
    public int Id { get; set; }
    public Agent Agent { get; set; }
    public BasicData BasicData { get; set; }
    public IEnumerable<string> NonOfferedProducts { get; set; }
}
//get json
string json = @"
    {
        ""insured_agent_flag"": ""a"",
        ""id"": ""1"",
        ""agent"": {
        ""fullName"": ""John Travolta"",
        ""mobileNumberPdf"": ""+987654321"",
        ""mobileNumber"": """",
        ""identityCard"": {
            ""identityCardExpirationDate"": null
        },
        ""secondIdentityCard"": {
            ""identityCardExpirationDate"": null
        },
        ""notes"": {},
        ""sign"": ""ADVANCED""
        },
        ""basicData"": {
                ""personType"": ""PERSON"",
                ""agreeWithCompleteAnalysis"": false,
                ""investmentInterest"": false
        },
        ""nonOfferedProducts"": [
        ""PROD_A"",
        ""PROD_B"",
        ""PROD_C""
        ]
    }";

var js = new JavaScriptSerializer();
ParentObject obj = js.Deserialize<ParentObject>(json);

//do things...
var rows = obj.NonOfferedProducts.ToList();
Assert.AreEqual(3, rows.Count);
Assert.AreEqual("PROD_A", rows.First());