1

My JSON has this information:

[
 {
  "era_1":{"technology_cost":"10000"},
  "era_2":{"technology_cost":"15000"},
  "era_3":{"technology_cost":"20000"},
  "era_4":{"technology_cost":"25000"},
  "era_5":{"technology_cost":"30000"}
 }
]

I want to do:

EraData = JsonConvert.DeserializeObject<List<ClassEra>>(JSON);

Being ClassEra

public class ClassEra
{
    public string name { get; set; }
    public string technology_cost { get; set; }
}

And obviously it doesn't work.

I don't understand the type of data that is coming out of the deserializer.

By the way I'm using Newtonsoft.

dbc
  • 104,963
  • 20
  • 228
  • 340
Filipe
  • 146
  • 10
  • You need to account for the `"era_X"` names. I reckon those are runtime-only names so use `JsonConvert.DeserializeObject>>(JSON);`. See as shown in [Deserializing JSON when key values are unknown](https://stackoverflow.com/a/24901245/3744182). – dbc Oct 23 '22 at 17:51
  • At first glance, it looks like you are declaring your class wrongly. The JSON is a list of 1 object which then has multiple objects. – elfico Oct 23 '22 at 17:53
  • @dbc i dont know the number of era_x that are coming so what sould i do? – Filipe Oct 23 '22 at 17:55
  • 2
    @Filipe - I told you. Use `JsonConvert.DeserializeObject>>(JSON)`. The `era_X` names will be deserialized as the dictionary keys, the `{"technology_cost":"XX"}` objects as the dictionary values. Also you can remove `name` from `ClassEra`. – dbc Oct 23 '22 at 17:57

3 Answers3

1

The json format is not what you are trying to capture as class object. Your targeted json should be :

[{"name":"era_1","technology_cost":"10000"},
{"name":"era_2","technology_cost":"15000"},
{"name":"era_3","technology_cost":"20000"},
{"name":"era_4","technology_cost":"25000"},
{"name":"era_5","technology_cost":"30000"}]
JemHah
  • 434
  • 4
  • 16
0

try this one

EraData  = JsonConvert.DeserializeObject<List<Dictionary<string, ClassEra>>>(JSON);

And class will be

    public class ClassEra
    {
        public string technology_cost { get; set; }
    }

Fetching details

I prefer to use dictionary because time complexity of fetching value is O(1) by key like dic[key]

//By this :- get complete dictionary
var dic = EraData[0]; //and iterate by foreach loop 
 
//by this you can get all technology cost:- 
var technology_cost = EraData.SelectMany(x => x.Values);

Screenshots

Dictionary enter image description here

Technology Cost enter image description here

Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21
0

To expand a bit on what @Pradeep wrote, if you want to know the era name you could select it like this

var dict = JsonConvert.DeserializeObject<List<Dictionary<string, ClassEra>>>(json);

var eras = dict.SelectMany(x => x.Keys.Select(k => new ClassEra()
{
    Name = k,
    TechnologyCost = x[k].TechnologyCost
}));

public class ClassEra
{
    public string Name { get; set; }
    [JsonProperty("technology_cost")] public string TechnologyCost { get; set; }
}
LLL
  • 3,566
  • 2
  • 25
  • 44
  • Thank you so much, i was tryind to what dbc and Pradeep said but couldnt get the "technology_cost" out. Is it because the _ is not supported? – Filipe Oct 23 '22 at 18:27
  • It's supported, it's just the structure was not the same as in your class. In C# the convention is to name things in `PascalCase` and not `snake_case`, that's why I changed it, but it still works with both. – LLL Oct 24 '22 at 08:21