0

I have a api that return data in this format

 { "reult": 
   { "70": 
     { 
       "type_id": 3, 
       "type": "forex", 
       "group_title": "forex", 
       "name": "EUR", 
       "title": "EUR" 
     }, 
     "724": 
      { 
        "type_id": 5, 
        "type": "shares", 
        "group_title": "shares", 
        "name": "#ABT", 
        "title": "#ABT" 
     } 
   }
 } 

Now I want these key object pair data to a genuine C# object array. Like this

 [
  {
    Id = 70
    Type_id = 3,
    Type = "forex",
    Group_title = "forex",
    Name = "EUR",
    Title = "EUR"
  },
  {
    Id = 724,
    Type_id = 5,
    Type = "shares",
    Group_title = "shares",
    Name = "#ABT",
    Title = "#ABT"
  }
]

Is it possible to do so? [I have Updated the api returned data. Because with this format it is easy to desterilize this data with c# Dictionary]

Udin M
  • 51
  • 5
  • The top one is not valid JSON data. Are you sure that is the way the API is returning the data? – Steve Dec 15 '21 at 06:33
  • Yes I just remove the result property it was { "reult": { "70": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" }, "724": { "type_id": 5, "type": "shares", "group_title": "shares", "name": "#ABT", "title": "#ABT" } }} – Udin M Dec 15 '21 at 06:36
  • Are you looking for JavaScript or C# code? – Steve Dec 15 '21 at 06:42
  • @Steve Looking for C# code – Udin M Dec 15 '21 at 07:19
  • Gotcha. The post is tagged with javascript as well as C#. So, I was unsure – Steve Dec 15 '21 at 07:40

3 Answers3

0

create class with those properties then use below code:

JsonSerializer.Deserialize<List<object>>(jsonString);

here is link to more detail:

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0

0

Say you have some classes:

public class Root
{
    [JsonProperty("result")]
    public Dictionary<int, Data> Result {get;set;}
}

public class Data
{
    [JsonIgnore]
    public int Id { get; set; }

    [JsonProperty("type_id")]
    public int TypeId { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("group_title")]
    public string GroupTitle { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }
}

You can deser the original data (not the one you modified by removing "result") and then call, if you want an array out of it:

var root = JsonConvert.DeserializeObject<Root>(str);
foreach(var kvp in root.Result) kvp.Value.Id = kvp.Key;
var arr = root.Result.Values.ToArray();

ps; you need references to Newtonsoft.Json; System.Collections.Generic; System.Linq;

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Not what I want, I tried it but you know this is not an object "70": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" } if the format is like this then your code will work. { "70": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" } } – Udin M Dec 15 '21 at 07:22
  • I did say **the original data**, i.e. what you said in your comment: *Yes I just remove the result property it was { "reult": { "70": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" }, "724": { "type_id": 5, "type": "shares", "group_title": "shares", "name": "#ABT", "title": "#ABT" } }}* - **don't remove the "result" property**; it damages your json and makes it invalid. If you've already removed it e.g. manually and saved the file and can never get it again, then you'll need to add it back manually – Caius Jard Dec 15 '21 at 07:26
  • Code works fine: https://dotnetfiddle.net/nKayIb – Caius Jard Dec 15 '21 at 07:27
0

When i deserialized your json to c# class it is right class object maybe your json is not correct

it is c# class:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class _70
    {
        public int type_id { get; set; }
        public string type { get; set; }
        public string group_title { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

    public class _724
    {
        public int type_id { get; set; }
        public string type { get; set; }
        public string group_title { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

    public class Reult
    {
        public _70 _70 { get; set; }
        public _724 _724 { get; set; }
    }

    public class Root
    {
        public Reult reult { get; set; }
    }