简体   繁体   English

How to Convert Json Array to Single JSON object without using T object in C#?

[英]How to Convert Json Array to Single JSON object without using T object in C#?

I am looking for a one-liner that transforms the Json array [{},{}] into object => [{}].我正在寻找一种将 Json 数组 [{},{}] 转换为 object => [{}] 的单线器。

I have the following serialized JSON string,我有以下序列化的 JSON 字符串,

 [
  {
    "Address": "Adresse"
  },
  {
    "AddressDetails": "Détails de l'adresse"
  },
  {
    "BackToLoginPage": "Retour à la page de connexion"
  }
]

Now I want to convert this into the following format without using one type or model object because the data is dynamic and it's going to contain 1000 number of fields so, I can not create a model type object with dynamic properties. Now I want to convert this into the following format without using one type or model object because the data is dynamic and it's going to contain 1000 number of fields so, I can not create a model type object with dynamic properties. So is there any idea to convert it into the one single JSON object/string?.那么有没有办法将它转换成一个单一的 JSON 对象/字符串? It looks simple, but I couldn't convert it.它看起来很简单,但我无法转换它。

[
    "Address": "Adresse",      
    "AddressDetails": "Détails de l'adresse",      
    "BackToLoginPage": "Retour à la page de connexion"
]

Newtonsoft.Json.Linq can parse/process JSON without model (JObject/JArray). Newtonsoft.Json.Linq 可以解析/处理 JSON 没有 Z20F35E630JFArray394DBFA8C3ObjectJF6) Here is one liner:这是一个班轮:

var mergedJson = new JObject(JArray.Parse(json).OfType<JObject>().SelectMany(o => o.Properties()).GroupBy(p => p.Name).Select(grp => grp.First())).ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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