【发布时间】:2018-11-05 21:28:31
【问题描述】:
我有一个模型类(Json 转换为 C# 类),其中我有一个数组及其属性。
现在的问题是我需要设置这个数组的值 atrributes 即 fieldId 和 fieldValue 存在于名为 optionalFields 的数组中。
提醒一下,我已经编写了一个构造函数,我可以在其中初始化变量,这是一个直接的镜头,但我尝试了几种方法来设置数组属性的值,但我实际上是在浪费时间。
下面是 Json :
{
"entityId": "1234",
"inventoryId": "4567",
"saleAccount": "SAA",
"saleGroup": "SLL",
"inventoryAccount": "IA1000",
"workInProcess": 0,
"isGlApplied": false,
"optionalFields": [
{
"fieldId": "29",
"fieldValue": "290"
}
],
}
下面是上述json的模型类:
public class Acct_Det_Widget_Post_Insert_Json
{
public string entityId { get; set; }
public string inventoryId { get; set; }
public string saleAccount { get; set; }
public string saleGroup { get; set; }
public string inventoryAccount { get; set; }
public int workInProcess { get; set; }
public bool isGlApplied { get; set; }
public Optionalfield[] optionalFields { get; set; }
public string vehicleId { get; set; }
public Acct_Det_Widget_Post_Insert_Json()
{
this.entityId = "1234";
this.inventoryId = "34521";
this.saleAccount = "SAA";
this.saleGroup = "SLL";
this.inventoryAccount = "IA1000";
this.workInProcess = 0;
this.isGlApplied = false;
}
}
public class Optionalfield
{
public string fieldid { get; set; }
public string fieldvalue { get; set; }
}
我可以设置其他字段的值,但我无法设置此 OptionalField 数组属性的值。
请尽快帮我解决问题。
【问题讨论】:
-
你试过直接 JSON 反序列化吗? new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize
("你的 JSON 字符串变量放在这里"); -
“我无法为...设置值”是什么意思?为什么?你试过什么?你有任何错误或异常吗?
-
我知道 JSON 反序列化的部分,但由于我是一只刚开始学习 C# 的新蜜蜂,我在设置数组属性的值时被打动了
-
我错过了没有初始化数组,从java到C#的语法有点不同,开始习惯了。