如何基于JSON中的值将值绑定到JSON文件? [英] How to bind values to JSON file based on values in JSON?

查看:151
本文介绍了如何基于JSON中的值将值绑定到JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户向我发送了一个JSON文件,如下所示:

My client sends me a JSON file which as follows:

{  
   "onlineRequest":{  
      "MobileNumber":"75484568",
      "ProductNo":"100",
      "JsonFile":{  
         "evaluation":{  
            "number":[  
               {  
                  "@para-id":"5656",
                  "@new-code":"",
                  "@text":"Hello America"
               },
               {  
                  "@para-id":"3302",
                  "@new-code":"100",
                  "@text":"Hello UK"
               }
            ],
            "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":"0"
                     }
                  ]
               },
               {  
                  "@para-id":"2575",
                  "@new-code":"",
                  "@text":"This is third dropdown",
                  "option":[  
                     {  
                        "@text":"Available",
                        "@value":"0"
                     }
                  ]
               }
            ]
         }
      }
   }
}

现在我需要根据其某些参数将值设置为此JSON文件.此JSON文件包含名为JsonFile的内部JSON.在number部分,如果

Now I need to set the values to this JSON file based on its some parameters.This JSON file consists with Inner JSON called JsonFile. In the number part, If

如果@ para-id = 5656,则@ new-code ="0000"

if @para-id = 5656, then @new-code = "0000"

如果@ para-id = 3302,则@ new-code ="1111"

if @para-id = 3302, then @new-code = "1111"

如果@ para-id = 3585,则@ new-code ="2222"

if @para-id = 3585, then @new-code = "2222"

为此,我的C#代码如下,source表示JSON字符串.

To achieve this, My C# code as follows, source means JSON string.

var json = JToken.Parse(source);
var dict = new Dictionary<string, string>
{
    ["5656"] = "0000",
    ["3302"] = "1000",
};

foreach (var number in json.SelectToken("onlineRequest.JsonFile.evaluation.number").ToArray())
{
    var id = (string)number["@para-id"];
    if (id != null && dict.TryGetValue(id, out var code))
    {
        number["@new-code"] = code;
    }
}

上面的代码对于number部分工作正常.但是问题出在dropdown部分.现在,我需要将值设置为JSON中的dropdown部分,如下所示

This above code works fine for number part. But the problem is in the dropdown part. now I need to set values to dropdown part in the JSON as follows

已更新:

如果@ para-id = 2572,
如果@value = 0,则@ new-code = 50,

if @para-id = 2572,
if there @value = 0 then @new-code = 50,

如果@value = 1,则@ new-code = 100,

if there @value = 1 then @new-code = 100,

如果@value = 2,则@ new-code = 150,

if there @value = 2 then @new-code = 150,

如果@ para-id = 2584,
如果@value = 0,则@ new-code = 10,

if @para-id = 2584,
if there @value = 0 then @new-code = 10,

如果@value = 1,则@ new-code = 20,

if there @value = 1 then @new-code = 20,

如果@value = 2,则@ new-code = 30,

if there @value = 2 then @new-code = 30,

如果@ para-id = 2575,
在那里@value = 0,然后@ new-code = 40,

if @para-id = 2575,
in there @value = 0 then @new-code = 40,

其中@value = 1,然后@ new-code = 80,

in there @value = 1 then @new-code = 80,

期望的输出:

"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":"80",
      "@text":"This is third dropdown",
      "option":[  
         {  
            "@text":"Not Available",
            "@value":"1"
         }
      ]
   }
]

我对此很困惑,所以请帮助我解决这个问题.如何如上所述设置值.提前致谢.

I'm so confused with this, So please help me to solve this problem. How can I set values as above. Thanks in advance.

推荐答案

与现有想法相同,只是这次您需要使用自定义函数(在本例中为Func<int, int>)来计算新值:

Same idea as the existing, except you would need to use a custom function (Func<int, int> in this case) to compute the new value this time:

var dropdownNewCodeMapping = new Dictionary<int, Func<int, int>>
{
    [2572] = x => (x + 1) * 50,
    [2584] = x => (x + 1) * 10,
    [2575] = x => (x + 1) * 40,
};
foreach (var dropdown in json.SelectToken("onlineRequest.JsonFile.evaluation.dropdown").ToArray())
{
    var id = (int)dropdown["@para-id"];
    var value = (int)dropdown.SelectToken("option[0].@value");

    dropdown["@new-code"] = dropdownNewCodeMapping[id](value).ToString();
}

这篇关于如何基于JSON中的值将值绑定到JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆