简体   繁体   中英

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.

I have the following json structure:

[{"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 . Create an entity 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()};

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