我对这个json脚本有一些困难:
c_int
我想从这个脚本中获取一些参数并将其放入sql server表中。 为了做到这一点,我使用并转换了https://mycontraption.com共享的C#脚本:
{
"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"
]
}
对于'标准'对象工作正常,但是数组" nonOfferedProducts"存在问题。编译后我收到一个错误: “对象引用未设置为对象的实例”。
以下是我的问题: 1.我应该如何处理非提供产品' C#脚本中的数组? 2.为什么我会收到上述错误? 3.遗憾的是,json脚本存在一些错误,例如缺少大括号。我该怎么处理?
谢谢!
非常感谢您的回答。根据您的意见,我会尝试给您更多解释: 1.我在这篇文章中添加的json脚本 - 它只是整个脚本的一小部分。在完整的脚本中有很多不同的参数。更重要的是,我的C#代码应扫描大约40.000个json脚本(存储在一列中的sql server表中)。这些脚本具有类似的结构 - 但不一样。 所以我想到了C#解决方案,它将搜索我需要的参数。对于没有这些参数的json脚本,c#代码会将空值放入正确的输出列。
以下是我的输出列: -agreeWithCompleteAnalysis -inOtherSystem -investmentInterest -projectionId -insuredAgentFflag -nonOfferedProducts
我明白,我班级的结构是错误的 - 我会改进它。 但我有一个疑问 - 是否有可能准备c#代码结构,只处理我需要的这些参数?
最后,我想把结果放到我的数据库中。 例如,如果nonOfferedProducts属性将具有3个值(并非总是!),我想向我的数据库表3发送记录(nonOfferedProducts列的3个不同值,3个其余列的相同值-agreeWithCompleteAnalysis,inOtherSystem等) )。
我希望现在能够清楚。 非常感谢你的帮助!
Ĵ
答案 0 :(得分:3)
使用https://quicktype.io并粘贴json,它将生成c#模型和序列化代码。
答案 1 :(得分:2)
正如我在评论中所说,你的c#模型与JSON对象不匹配。
如果模型由各种嵌套对象组成,以更好地反映实际的JSON,那么您将获得更多运气:
print (df)
group obsnum score
0 a 1 0.374540
4 b 1 0.156019
10 c 3 0.020584
一旦模型正确,那么反序列化对我来说对我来说没问题(我在单元测试中做了这个,但假设你的字符串符合你的例子,这应该没问题)
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());
属性中带有给定示例的字符串列表。
显然,如果你不能依赖JSON的一致性(无论是结构还是结构如何)那么你就会遇到问题,但这是一个不同的问题。
答案 2 :(得分:0)
回答你的问题没有2)你得到对象引用错误,因为BasicDataClass.nonOfferedProducts为null并且你正在尝试迭代它,这可能是你发送错误的json的原因,JavaScriptSerializer不能deserilize。
你的第3个问题你可以随时使用json验证器来验证你的json,这些验证器在网上有https://jsonformatter.org/