【问题标题】:How to set value to JSON to based on @value如何基于 @value 将值设置为 JSON
【发布时间】:2018-07-29 06:28:42
【问题描述】:

我从一些 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; }
}

【问题讨论】:

  • 根据您的 api json 创建模型并将 json 对象转换为您的模型类型,然后您可以在下拉列表中进行 foreach 并有条件地设置新代码
  • @HiteshKansagara 你能帮我这样做吗,我不知道这样做。请先生
  • @HiteshKansagara 我添加了一个模型类。这是正确的吗?

标签: c# json dictionary parameters json.net


【解决方案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; }
}

【讨论】:

  • Wooow 真棒先生 :) 你拯救了我的一部分生命。您能告诉我如何根据@value 将值设置为@new-code 请给我一个示例代码先生。请
  • @Gamma 更新了解决方案,希望得到投票和选定的答案。祝你好运
  • 提前感谢您的热心回复先生。我是 C# 的新手。我没有这样做的想法。在那个 json 中,有多个 @para-id..like 2575,2584。所以,那么我需要为每个@para-id 检查@value。所以我必须根据@value 设置@new-code。我现在就在那里。如何做到这一点
  • 不,先生! :(但由于您的友好回复,我接受了答案:)如果可以请帮我解决这个问题
  • 好的,只需复制粘贴最后一部分,这样就可以了
猜你喜欢
  • 2015-06-03
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2019-11-05
  • 2020-03-09
  • 1970-01-01
相关资源
最近更新 更多