简体   繁体   中英

Merge JSON string list result from into a single JSON with one root

I have list JSON string, I want to merge them into a single JSON array.

I am getting a result from an API and need to iterate the result string to get one single result string

I have a list string and I need to iterate it and convert the list string into a single string with one root element and result merged with in it

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

Solution 1: LINQ approach

  1. In jsonResult list, parse each item (JSON string) to JObject .

  2. From result 1, extract the value of arrayResult as JToken . It returns the items in arrayResult array as List<JToken> .

  3. Create an object with arrayResult property which holds the result from 2 .

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

Demo Solution 1 @ .NET Fiddle


Solution 2: foreach loop approach

  1. Create an object with arrayResult property which holds the array.

  2. Iterate each element in jsonResult , parse JSON string to JObject , extract the arrayResult value from JObject . This returns list of items. Then you need to .AddRange() to add the items list to result.arrayResult .

dynamic result = new {
    arrayResult = new List<dynamic>()   
};
        
foreach (var eachJson in jsonResult)
{
    result.arrayResult.AddRange(JObject.Parse(eachJson)
                                .SelectToken("arrayResult")
                                .ToList());
}

Demo Solution 2 @ .NET Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM