How to bind values to JSON file based on values in JSON?

旧巷老猫 提交于 2020-01-25 05:19:31

问题


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"
                     }
                  ]
               }
            ]
         }
      }
   }
}

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

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

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

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

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;
    }
}

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

UPDATED:

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

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

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

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

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

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

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

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

EXPECTED OUTPUT:

"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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/51559067/how-to-bind-values-to-json-file-based-on-values-in-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!