如何合并多个json对象

时间:2016-03-22 06:44:22

标签: c# asp.net json json.net

我使用不同的网址获取类似的json数据。

这样我就可以在同一个模型中获得更多信息

长期困扰我

我试图将json.net用于单个列表。

string url1= "https://apt.data2.com";
string url2= "https://apt.data1.com";
var json1= webClient.DownloadString(url1);
var json2= webClient.DownloadString(url2);

这些调用返回多个具有相同结构的json对象

    {
        data: [
             {
               created_time: "1457332172",
              text: "什麼東西",
              from: {
              username: "d86241",
              profile_picture: "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11371189_421316874725117_327631552_a.jpg",
              id: "397355082",
              full_name: "Jhao-wei Hvang"
    },
    id: "1200511729352353899"
      }
   ]
}

{
        data: [
             {
               created_time: "1111",
              text: "hi",
              from: {
              username: "22",
              profile_picture: "",
              id: "ss",
              full_name: "Hvang"
    },
    id: "1200511352353899"
      }
   ]
}

我想将这些对象组合起来生成

  {
    data:[
    {
      created_time:"1234"
      text:...
       ....
        ......
        },
    id:1234....
      ]

    data:[
    {
      created_time:"4567"
      text:....
       ....
        ......
        },
    id:4567....
      ]
    }

如何将这些合并到一个json对象中?

@foreach (var item in Model)
     {
@ Item.text
    }

1 个答案:

答案 0 :(得分:0)

var jObject1 = // Your first json object as JObject
var jObject2 = // Your second json object as JObject 
jObject1.Merge(jObject2);