Reputation: 1569
I have ran into an issue while pulling data down from an API (Not mine and I can't change the way its formatted) and then mapping that onto a C# List.
A small sample of the JSON looks like this (There are thousands of records in the form_values section and thousands of records)
JSON Sample:
{
"records":[
{
"status":"4",
"version":5,
"form_values":{
"4015":"TextValue",
"5919":"TextValue",
"6127":"TextValue",
"7868":"0",
"q311":"TextValue",
"r83b":"0"
}
}
],
"current_page":1,
"total_pages":1,
"total_count":1,
"per_page":12
}
Currently I am putting the JSON result into a strongly typed list with the following code (This uses Newtonsoft.Json):
C# JSON Deserialize to List
JsonConvert.DeserializeObject<Data>(json_data);
It is in the Data.cs Class that I am having issues as C# will not allow a variable to start with a int.
What I have tried, with no success is using the Runtime.Serilization DataMember
attribute.
Data.cs
[DataMember(Name = "4015")]
public string textfeild { get; set; }
The Issue:
This results in a null value being set regardless of data behind. All of the fields that start with a letter work fine, so I am confident my JSON is being written to the List correctly, just need a solution for these annoying ID's that start with a number!
Any help would be much appreciated.
Upvotes: 0
Views: 259
Reputation: 1201
Use List<KeyValuePair<string, string>>
var dictionary = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(dict)
.ToDictionary(x => x.Key, y => y.Value);
Use a custom object that represents your pairs and then create a dictionary from your collection.
var output = JsonConvert.DeserializeObject<List<Temp>>(dict);
var dictionary = output.ToDictionary(x => x.Key, y => y.Value);
public class Temp
{
public string Key { get; set; }
public string Value { get; set; }
}
also you can get a reference from Here
Upvotes: 0
Reputation: 18295
You have two options, the first one is to use JsonProperty
attribute:
public class FormValues
{
[JsonProperty(PropertyName = "4015")]
public string T4015 { get; set; }
[JsonProperty(PropertyName = "5919")]
public string T5919 { get; set; }
[JsonProperty(PropertyName = "6127")]
public string T6127 { get; set; }
[JsonProperty(PropertyName = "7868")]
public string T7868 { get; set; }
public string q311 { get; set; }
public string r83b { get; set; }
}
public class Record
{
public string status { get; set; }
public int version { get; set; }
public FormValues form_values { get; set; }
}
public class Data
{
public List<Record> records { get; set; }
public int current_page { get; set; }
public int total_pages { get; set; }
public int total_count { get; set; }
public int per_page { get; set; }
}
The second one is to use a Dictionary<string, string>
for your form_values
property:
public class Record
{
public string status { get; set; }
public int version { get; set; }
public Dictionary<string, string> form_values { get; set; }
}
public class Data
{
public List<Record> records { get; set; }
public int current_page { get; set; }
public int total_pages { get; set; }
public int total_count { get; set; }
public int per_page { get; set; }
}
If your form_values
are not fixed, then you should stick with the second option.
Upvotes: 2
Reputation: 4423
Create a property for form_values
as Dictionary
public Dictionary<string, string> form_values { get; set; }
Upvotes: 1