问题
I know there have been some questions already raised about this topic however I still struggle o understand it. I have quite a complex JSON object returning which has a dynamic key at the beginning. I am trying to deserialize it into a C# object model but my problem is the dynamic key:
{
"ISBN:0903393972": {
"bib_key": "ISBN:0903393972",
"details": {
"publishers": ["Sweet & Maxwell"],
"physical_format": "Hardcover",
"title": "Tanker Voyage Charter Parties",
"created": {
"type": "/type/datetime",
"value": "2008-04-30T09:38:13.731961"
},
"authors": [{
"name": "F.M. Ventris",
"key": "/authors/OL3610236A"
}],
}}}
My headache comes from the "ISBN:0903393972" part which is dynamic. Below is also part of the model:
[DataContract]
public class Created
{
[DataMember]
public string type { get; set; }
[DataMember]
public DateTime value { get; set; }
}
[DataContract]
public class Author
{
[DataMember]
public string name { get; set; }
[DataMember]
public string key { get; set; }
}
[DataContract]
public class Details
{
[DataMember]
public List<string> publishers { get; set; }
[DataMember]
public string physical_format { get; set; }
[DataMember]
public string title { get; set; }
[DataMember]
public Created created { get; set; }
[DataMember]
public List<Author> authors { get; set; }
}
[DataContract]
public class ISBN
{
[DataMember]
public string bib_key { get; set; }
[DataMember]
public Details details { get; set; }
}
[DataContract]
public class RootObject
{
public Dictionary<string, ISBN> bookRoot { get; set; }
}
I am deserializing the JSON format with the below code (which works in case I hard code the "ISBN:0903393972" as the key of the object):
string json = new WebClient().DownloadString(URLAddress);
JObject book = JObject.Parse(json) as JObject;
// Copy to a static Album instance
RootObject deserializedBook = book.ToObject<RootObject>();
return deserializedBook;
I am sure that I'm missing something here but have been struggling he whole day already in trying to find a solution. Any help would be really appreciated!
回答1:
You are close.
In your JSON, the dynamic keys are at the root level, and so that object needs to be represented by a Dictionary<string, ISBN>
. It seems you have realized this, however, in your model, you have added an outer class to contain the dictionary. This doesn't line up with the JSON and is not needed here. Instead, you should deserialize directly into the dictionary:
string json = new WebClient().DownloadString(URLAddress);
var bookRoot = JsonConvert.DeserializeObject<Dictionary<string, ISBN>>(json);
From there, you can loop over the Values
collection on the dictionary to access the books without having to know the key(s) in advance:
foreach (var isbn in bookRoot.Values)
{
// ...
}
Or, if you're expecting only one book, you can get it from the dictionary like this:
var isbn = bookRoot.Values.First();
Here is a working demo: https://dotnetfiddle.net/csgSKp
来源:https://stackoverflow.com/questions/54611199/deserialize-json-with-dynamic-key-into-object