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]

5
  • The top one is not valid JSON data. Are you sure that is the way the API is returning the data? Commented Dec 15, 2021 at 6: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" } }} Commented Dec 15, 2021 at 6:36
  • Are you looking for JavaScript or C# code? Commented Dec 15, 2021 at 6:42
  • @Steve Looking for C# code Commented Dec 15, 2021 at 7:19
  • Gotcha. The post is tagged with javascript as well as C#. So, I was unsure Commented Dec 15, 2021 at 7:40

3 Answers 3

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;

Sign up to request clarification or add additional context in comments.

3 Comments

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" } }
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
Code works fine: dotnetfiddle.net/nKayIb
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

1 Comment

This is not right answer. There is a id as key of each object so your answer won't work.
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; }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.