简体   繁体   English

使用 C# 从反序列化的 json 创建一个新数组

[英]Using C# to create a new array from deserialized json

I am trying to use C# to restructure a JSON response into a more desirable format.我正在尝试使用 C# 将 JSON 响应重组为更理想的格式。

I have the following json structure:我有以下 json 结构:

[{"id":"1127889"},{"id":"1075442"}, {"id":"1201544"}]

I have used the following code to deserialize it:我使用以下代码对其进行反序列化:

var IDList = json.Select(JsonConvert.DeserializeObject<IDList>)

I am trying to restructure the data in the following format:我正在尝试以以下格式重组数据:

{"ID":["123235", "nvnhlkisd", "1812dhd"]}

I am having trouble with task and have tried many options to no avail.我在执行任务时遇到了麻烦,并且尝试了许多选项都无济于事。

Help would be appreciated.帮助将不胜感激。 Thanks.谢谢。

Here is how you could approach this with Newtonsoft.Json .这是使用Newtonsoft.Json解决此问题的方法。 Create an entity class:创建实体 class:

class Entity
{
   [JsonProperty("id")]
   public string Id;
}

Then get a list of entities:然后获取实体列表:

var json = "[{\"id\":\"1127889\"},{\"id\":\"1075442\"}, {\"id\":\"1201544\"}]";
var workingObject = JsonConvert.DeserializeObject<List<Entity>>(json);

var idList = new { id = (from c in workingObject select c.Id).ToArray()};

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

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