【问题标题】:Add array tag into JSON string value using c#使用 c# 将数组标签添加到 JSON 字符串值中
【发布时间】:2021-12-28 07:36:14
【问题描述】:

我得到了一个 JSON 字符串值:

{"refKey":"1","recordType":"address","bureauType":"consumer","controlData":{"success":"true"},"candidateRecordScores":[{"bestScore":"7","inputRecordScores":"7"}]}

我想将 inputRecordScores 值内的 candidateRecordScores 节点更改为数组格式。

下面我需要的输出格式:

"inputRecordScores":[7]

还有 candidateRecordScores 子元素值作为字符串格式更改为 INT

下面我需要的最终输出:

{"refKey":"1","recordType":"address","bureauType":"consumer","controlData":{"success":"true"},"candidateRecordScores":[{"bestScore":7,"inputRecordScores":[7]}]} 

这是我的代码如下所示:

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using System;

using System.IO;

using System.Net;


amespace CAAS_SoftCIR

{

    public class Class1

    {
        static void Main(string[] args)

        {
            string strJson = @"{

                                  'ExpressMatch': {

                                    'refKey': '1',

                                    'recordType': 'address',

                                    'bureauType': 'consumer',

                                    'controlData': {

                                      'success': 'true'

                                    },

                                    'candidateRecordScores': [

                                      {

                                        'bestScore': '7',

                                        'inputRecordScores': '7'

                                      }

                                    ]

                                  }

                                }";


            JObject rss = JObject.Parse(strJson);

            //var rssTitle = Convert.ToString(rss["ExpressMatch"]);

            var rssTitle = (object)rss["ExpressMatch"];

            var json = JsonConvert.SerializeObject((object)rss["ExpressMatch"] , Formatting.None);

            Console.WriteLine(jsonNew);

        }


    }

}

【问题讨论】:

  • 请展示您的尝试。您似乎还没有尝试过任何东西。
  • 请参考我上面的代码 这是我的代码如下所示:@Llama
  • 我在上面看到了您的代码,但除了将 JSON 解析为 JObject(很好的第一步)之外,似乎与您发布的任务无关。是什么阻止您遍历 candidateRecordScores 中的项目,获取 inputRecordScores 的值,然后用该值的数组替换属性值?
  • 伪代码:foreach (JObject childObj in rss["ExpressMatch"]["candidateRecordScores"]) { currentValue = childObj["inputRecordScores"]; int parsedValue = int.Parse(currentValue); childObj["inputRecordScores"] = new JArray() { parsedValue }; }
  • 嗨@Llama,您能否将代码添加到答案中,因为该概念已经过测试并且效果很好。谢谢。 Demo

标签: c# asp.net json .net


【解决方案1】:

在 Main 方法中尝试一下

string strJson = @"{
    'ExpressMatch': {
    'refKey': '1',
    'recordType': 'address',
    'bureauType': 'consumer',
    'controlData': {
        'success': 'true'
    },
    'candidateRecordScores': [
        {
            'bestScore': '7',
            'inputRecordScores': '7'
        }
    ]
    }
}";


JObject rss = JsonConvert.DeserializeObject<JObject>(strJson);

foreach(JObject score in rss["ExpressMatch"]["candidateRecordScores"]) {
    score["inputRecordScores"] = new JArray((int)score["inputRecordScores"]);
}

var newJson = JsonConvert.SerializeObject(rss, Formatting.Indented);

Console.WriteLine(newJson);

【讨论】:

  • 谢谢@Henry Tran。额外添加了一些标签,例如 EXpressMatch 父节点
猜你喜欢
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
相关资源
最近更新 更多