我从某些API收到了这种格式的JSON字符串。
{
"onlineRequest":{
"MobileNumber":"75484568",
"ProductNo":"100",
"JsonFile":{
"dropdown":[
{
"@para-id":"2572",
"@new-code":"",
"@text":"This is first dropdown",
"option":[
{
"@text":"Option 1",
"@value":"0"
}
]
},
{
"@para-id":"2584",
"@new-code":"",
"@text":"This is second dropdown",
"option":[
{
"@text":"Excellent",
"@value":"2"
}
]
},
{
"@para-id":"2575",
"@new-code":"",
"@text":"This is third dropdown",
"option":[
{
"@text":"Not Available",
"@value":"1"
}
]
}
]
}
}
}
为了此JSON字符串,我需要将值绑定到
JSON。每个参数ID都有单独的@value。这是动态的。 @value
的值可以是0、1、2、3、4
@ para-id = 2572时,
如果@value = 0,我需要将值传递给@ new-code = 50,
如果@value = 1,我需要将该值传递给@ new-code = 60,
如果@value = 2,我需要将该值传递给@ new-code = 70
@ para-id = 2584时,
如果@value = 0,我需要将值传递给@ new-code = 10,
如果@value = 1,我需要将该值传递给@ new-code = 20,
如果@value = 2,我需要将该值传递给@ new-code = 30,
当@ para-id = 2575时,
如果@value = 0,我需要将该值传递给@ new-code =“ A”,
如果@value = 1,我需要将该值传递给@ new-code =“ B”
预期输出:
"dropdown":[
{
"@para-id":"2572",
"@new-code":"50",
"@text":"This is first dropdown",
"option":[
{
"@text":"Option 1",
"@value":"0"
}
]
},
{
"@para-id":"2584",
"@new-code":"30",
"@text":"This is second dropdown",
"option":[
{
"@text":"Excellent",
"@value":"2"
}
]
},
{
"@para-id":"2575",
"@new-code":"B",
"@text":"This is third dropdown",
"option":[
{
"@text":"Not Available",
"@value":"1"
}
]
}
]
如何使用C#执行此操作。请帮我解决这个问题。
已更新:
为了JSON,我创建了一个模型类。但是问题是使用@ text,@ value和@ para-id时。我知道我无法创建这样的字段
public class Option
{
public string @text { get; set; }
public string @value { get; set; }
}
public class Dropdown
{
public string @para-id { get; set; }
public string @new-code { get; set; }
public string @text { get; set; }
public List<Option> option { get; set; }
}
public class JsonFile
{
public List<Dropdown> dropdown { get; set; }
}
public class OnlineRequest
{
public string MobileNumber { get; set; }
public string ProductNo { get; set; }
public JsonFile JsonFile { get; set; }
}
public class RootObject
{
public OnlineRequest onlineRequest { get; set; }
}
答案 0 :(得分:1)
您可以使用此方法反序列化JSON ...它将首先反序列化,投影Dropdown
元素并将其重新序列化为JSON格式
void Main()
{
string testJson = @"{""onlineRequest"":{""MobileNumber"":""75484568"",""ProductNo"":""100"",""JsonFile"":{""dropdown"":[{""@para-id"":""2572"",""@new-code"":"""",""@text"":""This is first dropdown"",""option"":[{""@text"":""Option 1"",""@value"":""0""}]},{""@para-id"":""2584"",""@new-code"":"""",""@text"":""This is second dropdown"",""option"":[{""@text"":""Excellent"",""@value"":""2""}]},{""@para-id"":""2575"",""@new-code"":"""",""@text"":""This is third dropdown"",""option"":[{""@text"":""Not Available"",""@value"":""1""}]}]}}}";
Response response = JsonConvert.DeserializeObject<Response>(testJson);
var dropdowns = response.OnlineRequest.JsonFile;
string json = JsonConvert.SerializeObject(dropdowns, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
}
public class Option
{
[JsonProperty("@text")]
public string Text { get; set; }
[JsonProperty("@value")]
public string Value { get; set; }
}
public class Dropdown
{
[JsonProperty("@para-id")]
public string ParaId { get; set; }
[JsonProperty("@new-code")]
public string NewCode { get; set; }
[JsonProperty("@text")]
public string Text { get; set; }
[JsonProperty("option")]
public IList<Option> Option { get; set; }
}
public class JsonFile
{
[JsonProperty("dropdown")]
public IList<Dropdown> Dropdown { get; set; }
}
public class OnlineRequest
{
[JsonProperty("MobileNumber")]
public string MobileNumber { get; set; }
[JsonProperty("ProductNo")]
public string ProductNo { get; set; }
[JsonProperty("JsonFile")]
public JsonFile JsonFile { get; set; }
}
public class Response
{
[JsonProperty("onlineRequest")]
public OnlineRequest OnlineRequest { get; set; }
}
输出看起来像这样
{
"dropdown": [
{
"@para-id": "2572",
"@new-code": "",
"@text": "This is first dropdown",
"option": [
{
"@text": "Option 1",
"@value": "0"
}
]
},
{
"@para-id": "2584",
"@new-code": "",
"@text": "This is second dropdown",
"option": [
{
"@text": "Excellent",
"@value": "2"
}
]
},
{
"@para-id": "2575",
"@new-code": "",
"@text": "This is third dropdown",
"option": [
{
"@text": "Not Available",
"@value": "1"
}
]
}
]
}
编辑:响应评论,将NewCode属性更改为此
void Main()
{
string testJson = @"{""onlineRequest"":{""MobileNumber"":""75484568"",""ProductNo"":""100"",""JsonFile"":{""dropdown"":[{""@para-id"":""2572"",""@new-code"":"""",""@text"":""This is first dropdown"",""option"":[{""@text"":""Option 1"",""@value"":""0""}]},{""@para-id"":""2584"",""@new-code"":"""",""@text"":""This is second dropdown"",""option"":[{""@text"":""Excellent"",""@value"":""2""}]},{""@para-id"":""2575"",""@new-code"":"""",""@text"":""This is third dropdown"",""option"":[{""@text"":""Not Available"",""@value"":""1""}]}]}}}";
Response response = JsonConvert.DeserializeObject<Response>(testJson);
var dropdowns = response.OnlineRequest.JsonFile;
string json = JsonConvert.SerializeObject(dropdowns, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
}
public class Option
{
[JsonProperty("@text")]
public string Text { get; set; }
[JsonProperty("@value")]
public string Value { get; set; }
}
public class Dropdown
{
[JsonProperty("@para-id")]
public string ParaId { get; set; }
[JsonProperty("@new-code")]
public string NewCode {
get{
if (this.Option.Count == 0)
{
return string.Empty;
}
return this.Option.First().Value;
}
}
[JsonProperty("@text")]
public string Text { get; set; }
[JsonProperty("option")]
public IList<Option> Option { get; set; }
}
public class JsonFile
{
[JsonProperty("dropdown")]
public IList<Dropdown> Dropdown { get; set; }
}
public class OnlineRequest
{
[JsonProperty("MobileNumber")]
public string MobileNumber { get; set; }
[JsonProperty("ProductNo")]
public string ProductNo { get; set; }
[JsonProperty("JsonFile")]
public JsonFile JsonFile { get; set; }
}
public class Response
{
[JsonProperty("onlineRequest")]
public OnlineRequest OnlineRequest { get; set; }
}