首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将JSON字符串列表合并为一个带有一个根的JSON

将JSON字符串列表合并为一个带有一个根的JSON
EN

Stack Overflow用户
提问于 2022-08-16 06:28:55
回答 1查看 80关注 0票数 0

我有list JSON字符串,我想将它们合并到一个JSON数组中。

我从API中得到一个结果,需要迭代结果字符串才能得到一个结果字符串。

我有一个列表字符串,我需要迭代它,并将列表字符串转换为一个包含一个根元素的字符串,并将其中的结果合并。

代码语言:javascript
代码运行次数:0
运行
复制
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            MergeJsonResult();
        }
        private static void MergeJsonResult()
        {
            List<string> jsonResult = new List<string>();
            jsonResult.Add("{\n  \"arrayResult\": [\n    {\"Item1\": \"123\",\n    \"Item2\": \"858\",\n    \"Item3\": \"789\"\n    },\n    {\"Item1\": \"2588\",\n    \"Item2\": \"858\",\n    \"Item3\": \"587\"\n    }\n  ]\n}");
            jsonResult.Add("{\n  \"arrayResult\": [\n    {\"Item1\": \"123\",\n    \"Item2\": \"858\",\n    \"Item3\": \"789\"\n    },\n    {\"Item1\": \"2588\",\n    \"Item2\": \"858\",\n    \"Item3\": \"587\"\n    }\n  ]\n}");
            jsonResult.Add("{\n  \"arrayResult\": [\n    {\"Item1\": \"123\",\n    \"Item2\": \"858\",\n    \"Item3\": \"789\"\n    },\n    {\"Item1\": \"2588\",\n    \"Item2\": \"858\",\n    \"Item3\": \"587\"\n    }\n  ]\n}");

            foreach (var eachJson in jsonResult)
            {
                //Wat to merge JSON string
            }
            //Result should be with one root "arrayResult" and with in that the result array merged
        }
    }
}

**Result**
*Input*

 - String1

{
  "arrayResult": [
    {
      "Item1": "123",
      "Item2": "858",
      "Item3": "789"
    },
    {
      "Item1": "2588",
      "Item2": "858",
      "Item3": "587"
    }
  ]
}
- String2

{
  "arrayResult": [
    {
      "Item1": "123",
      "Item2": "858",
      "Item3": "789"
    },
    {
      "Item1": "2588",
      "Item2": "858",
      "Item3": "587"
    }
  ]
}
- String3

{
  "arrayResult": [
    {
      "Item1": "123",
      "Item2": "858",
      "Item3": "789"
    },
    {
      "Item1": "2588",
      "Item2": "858",
      "Item3": "587"
    }
  ]
}

*Output*
- Merged string with one root 

{
  "arrayResult": [
    {
      "Item1": "123",
      "Item2": "858",
      "Item3": "789"
    },
    {
      "Item1": "2588",
      "Item2": "858",
      "Item3": "587"
    },
    {
      "Item1": "123",
      "Item2": "858",
      "Item3": "789"
    },
    {
      "Item1": "2588",
      "Item2": "858",
      "Item3": "587"
    },
    {
      "Item1": "123",
      "Item2": "858",
      "Item3": "789"
    },
    {
      "Item1": "2588",
      "Item2": "858",
      "Item3": "587"
    }
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-16 06:45:22

解决方案1: LINQ方法

  1. jsonResult列表中,将每个项(JSON )解析为JObject
  2. 从结果1中,提取arrayResult作为JToken的值。它将arrayResult数组中的项作为List<JToken>返回。
  3. 创建一个具有arrayResult属性的对象,该属性保存来自2的结果。
代码语言:javascript
代码运行次数:0
运行
复制
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

List<JToken> items = jsonResult.Select(x => JObject.Parse(x))
    .SelectMany(x => x["arrayResult"])
    .ToList();
        
dynamic result = new {
    arrayResult = items 
};

演示解决方案1@ .NET Fiddle

foreach 解决方案2: 循环方法

  1. 创建一个具有arrayResult属性的对象,该属性包含数组。
  2. 迭代jsonResult中的每个元素,将JSON字符串解析为JObject,从JObject中提取arrayResult值。这将返回项目列表。然后需要.AddRange()将项目列表添加到result.arrayResult中。
代码语言:javascript
代码运行次数:0
运行
复制
dynamic result = new {
    arrayResult = new List<dynamic>()   
};
        
foreach (var eachJson in jsonResult)
{
    result.arrayResult.AddRange(JObject.Parse(eachJson)
                                .SelectToken("arrayResult")
                                .ToList());
}

演示解决方案2@ .NET Fiddle

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73369637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档