C# parsing JSON array overwrites values on the list - c#

Im having problem with deserializing json file into my class.
Json file looks like this:
{
"DataFile": {
"header":{
"version": "123",
"date": "01.01.01",
},
"customer": {
"fID": "12-35-58",
"nameCust": "CompanyName",
"adressCust":{
"zip": "0000",
"city": "Foovile",
"streetNr": "1",
"post": "FoovilePost",
"street": "DunnoStr",
},
},
"content": [
{
"invoice":{
"DFID":"538",
},
"invoice":{
"DFID":"500",
},
"invoice":{
"DFID":"550",
},
"receipt":{
"DFID":"758",
},
"receipt":{
"DFID":"75",
},
}
],
}
}
Everything before content array deserializes fine,so to keep things clear I'll skip parts of my class. Relevant bit looks like this:
class DFFile
{
public DF Df { get; set; }
}
class DF
{
public Header header { get; set; }
public Customer customer { get; set; }
public List<DFContent> content { get; set; }
}
class DFContent
{
public Invoice invoice { get; set; }
public Receipt receipt { get; set; }
}
class Invoice
{
public int DFID { get; set; }
}
class Receipt
{
public int DFID { get; set; }
}
And i deserialize into DFFile instance like this:
DFFile sample = JsonConvert.DeserializeObject<DFFile>(json);
My problem is that it deserializes without errors but sample.DF.content have only one element that have invoice and receipt with last id of each type. And result I'm looking for is list where there is new element for each item of json content array.
I can change my class but way this json is build is set in stone, can't do anything about it and have to deal with it.
Is there any way to stop it changing last element of content and add new one instead?

Your content is set up to hold an array but you only have one item in it which has three invoice and two receipt values. Assuming you want your DFContent to hold either a receipt or invoice, change it to look like this:
"content": [
{"invoice":{
"DFID":"538",
}},
{"invoice":{
"DFID":"500",
}},
{"invoice":{
"DFID":"550",
}},
{"receipt":{
"DFID":"758",
}},
{"receipt":{
"DFID":"75",
}}
],

Since you cannot change the JSON I assumed that you can change your class structure.
Please consider this classes:
class DF
{
// Other properties...
[JsonConverter(typeof(FunnyListConverter))]
public List<Content> content { get; set; }
}
class Content
{
public int DFID { get; set; }
}
class Invoice : Content
{
}
class Receipt : Content
{
}
And here is the FunnyListConverter:
public class FunnyListConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var content = new List<Content>();
var currentType = "";
Invoice currentInvoice = null;
Receipt currentReceipt = null;
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndArray)
{
break;
}
if (reader.Value?.ToString() == "invoice")
{
currentInvoice = new Invoice();
currentType = "invoice";
}
if (reader.Value?.ToString() == "receipt")
{
currentReceipt = new Receipt();
currentType = "receipt";
}
if (reader.Path.Contains("DFID") && reader.Value?.ToString() != "DFID")
{
switch (currentType)
{
case "invoice":
currentInvoice.DFID = int.Parse(reader.Value.ToString());
content.Add(currentInvoice);
currentInvoice = null;
break;
case "receipt":
currentReceipt.DFID = int.Parse(reader.Value.ToString());
content.Add(currentReceipt);
currentReceipt = null;
break;
}
}
}
return content;
}
public override bool CanConvert(Type objectType)
{
return true;
}
if (reader.Value?.ToString() == "receipt")
{
currentReceipt = new Receipt();
currentType = "receipt";
}
if (reader.Path.Contains("DFID") && reader.Value?.ToString() != "DFID")
{
switch (currentType)
{
case "invoice":
currentInvoice.DFID = int.Parse(reader.Value.ToString());
content.Add(currentInvoice);
currentInvoice = null;
break;
case "receipt":
currentReceipt.DFID = int.Parse(reader.Value.ToString());
content.Add(currentReceipt);
currentReceipt = null;
break;
}
}
}
return content;
}
public override bool CanConvert(Type objectType)
{
return true;
}
}

Related

JSON complex type that can be an object or an array of objects [duplicate]

This question already has answers here:
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 3 years ago.
I am trying to process an object that can be either an array of object or just the object. Using the code below only works when the naics is an object and not an array. What am I doing wrong?
Here is the shortest example I can come up with:
[{
"section": "52.219-1.b",
"naics": [{
"naicsName": "Engineering Services",
"isPrimary": true,
"ExcpCounter": 1,
"isSmallBusiness": "Y",
"naicsCode": 541330
},
{
"naicsName": "Military and Aerospace Equipment and Military Weapons",
"isPrimary": true,
"ExcpCounter": 2,
"isSmallBusiness": "Y",
"naicsCode": 541330
}
]
},
{
"section": "52.219-1.b",
"naics": {
"naicsName": "Janitorial Services",
"isPrimary": true,
"isSmallBusiness": "Y",
"naicsCode": 561720
}
}
]
I will only have one of the types but I forced two in an array to force it into Quick Type.
My classes are:
[JsonProperty("naics", NullValueHandling = NullValueHandling.Ignore)]
public AnswerNaics Naics { get; set; }
public partial struct AnswerNaics
{
public AnswerNaic Naic;
public AnswerNaic[] NaicArray;
public static implicit operator AnswerNaics(AnswerNaic Naic) => new AnswerNaics { Naic = Naic };
public static implicit operator AnswerNaics(AnswerNaic[] NaicArray) => new AnswerNaics { NaicArray = NaicArray };
}
public partial class AnswerNaic
{
[JsonProperty("naicsName")]
public string NaicsName { get; set; }
[JsonProperty("hasSizeChanged")]
public string HasSizeChanged { get; set; }
[JsonProperty("isPrimary")]
public bool IsPrimary { get; set; }
[JsonProperty("ExcpCounter", NullValueHandling = NullValueHandling.Ignore)]
public long? ExcpCounter { get; set; }
[JsonProperty("isSmallBusiness")]
public string IsSmallBusiness { get; set; }
[JsonProperty("naicsCode")]
public string NaicsCode { get; set; }
}
internal class NaicsConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(AnswerNaics) || t == typeof(AnswerNaics?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<AnswerNaic>(reader);
return new AnswerNaics { Naic = objectValue };
case JsonToken.StartArray:
var arrayValue = serializer.Deserialize<AnswerNaic[]>(reader);
return new AnswerNaics { NaicArray = arrayValue };
}
throw new Exception("Cannot unmarshal type AnswerNaics");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (AnswerNaics)untypedValue;
if (value.NaicArray != null)
{
serializer.Serialize(writer, value.NaicArray);
return;
}
if (value.Naic != null)
{
serializer.Serialize(writer, value.Naic);
return;
}
throw new Exception("Cannot marshal type Naics");
}
public static readonly NaicsConverter Singleton = new NaicsConverter();
}
I have more object or array nodes, but I am just trying to figure out one to be able to apply to all of them.
Since you cannot change the incoming JSON, you're going to need a custom converter instead. Something like this for example:
public class NaicsConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(Naics);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
var naics = new Naics();
switch (reader.TokenType)
{
case JsonToken.StartObject:
// We know this is an object, so serialise a single Naics
naics.Add(serializer.Deserialize<Naic>(reader));
break;
case JsonToken.StartArray:
// We know this is an object, so serialise multiple Naics
foreach(var naic in serializer.Deserialize<List<Naic>>(reader))
{
naics.Add(naic);
}
break;
}
return naics;
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And the supporting classes:
public class Root
{
public string Section { get; set; }
[JsonConverter(typeof(NaicsConverter))]
public Naics Naics { get; set; }
}
// This isn't ideal, but it's quick and dirty and should get you started
public class Naics : List<Naic>
{
}
public class Naic
{
public string NaicsName { get; set; }
public bool IsPrimary { get; set; }
public string IsSmallBusiness { get; set; }
public long NaicsCode { get; set; }
}
And finally, to deserialise:
var settings = new JsonSerializerSettings {Converters = {new NaicsConverter()}};
var root = JsonConvert.DeserializeObject<Root[]>(Json, settings);
Now your object will get serialised into the list, but as a single item.
You can solve this using a dynamic field in your class.
Consider this JSON:
[
{
"field1": "val1",
"nested": [
{
"nestedField": "val2"
},
{
"nestedField": "val3"
}
]
},
{
"field1": "val4",
"nested":
{
"nestedField": "val5"
}
}
]
nested field is first an array with 2 objects and in the second appearance a single object. (similar to the JSON you posted)
So the class representation would look like:
public class RootObject
{
public string field1 { get; set; }
public dynamic nested { get; set; }
public List<NestedObject> NestedObjects
{
get
{
if(nested is JArray)
{
return JsonConvert.DeserializeObject<List<NestedObject>>(nested.ToString());
}
var obj = JsonConvert.DeserializeObject<NestedObject>(nested.ToString());
return new List<NestedObject> { obj };
}
}
}
public class NestedObject
{
public string nestedField { get; set; }
}
The Deserialization code is trivial using Newtonsoft JSON:
var objectList = JsonConvert.DeserializeObject<List<RootObject>>("some_json");
foreach(var v in objectList)
{
foreach(var n in v.NestedObjects)
{
Console.WriteLine(n.nestedField);
}
}
The only change is an implementation of NestedObjects ready only property. It check if the dynamic object is JArray or object. In any case, it returns a List of nested objects.

Deserializing JSON into an object having abstract and non-abstract classes

I want to deserialize the following JSON into my C# Tree object. The tree structure cannot be edited.
JSON:
{
"Root": {
"Type": 0,
"children": [
{
"Type": 1,
"Name": " SERVICES",
"children": [
{
"Type": 2,
"Name": " SERVICES",
"Code": "S01",
"children": [],
"leaves": [
{
"Type": 6,
"Code": "H-L-CWP-50",
"Uom": "SQM",
"Measurements": "SQM",
"BaseRate": "€20"
},
{
"Type": 6,
"Code": "HMS-REM-001-03",
"Uom": "SQ.M",
"Measurements": "SQ.M",
"BaseRate": "€6.38"
}
]
}
]
}
]
}
}
C# Tree:
public class Tree
{
public Root Root { get; set; }
public Tree()
{}
}
public class Root : Node
{
public override NodeType Type => NodeType.Root;
}
public class Group : Node
{
public override NodeType Type => NodeType.Group;
}
public class Category : Node
{
public override NodeType Type => NodeType.Category;
}
public class Type : Node
{
public override NodeType Type => NodeType.Type;
}
public class SubType : Node
{
public override NodeType Type => NodeType.SubType;
}
public class SubSubType : Node
{
public override NodeType Type => NodeType.SubsubType;
}
}
public abstract class Node
{
public int? ID { get; set; }
public int? FK { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public List<Node> children { get; set; }
public List<Item> leaves { get; set; }
}
public class Item
{
public string Code { get; set; }
public string Desc { get; set; }
public string Uom { get; set; }
public float? Measurements { get; set; }
public int? FK { get; set; }
}
and the Tree is designed in a fashion where a Node can have a list of other Nodes as well as a list of Items; children and leaves respectively in JSON.
With the custom converter I built, I can deserialize the Nodes whenever no leaves exist, but populated leaves makes the JSON unrecognizable and an exception is thrown.
public class NodeConverter : JsonCreationConverter<Node> //notice the Node type here where in fact its a mixture of Nodes and Items.
{
protected override Node Create(Type objectType, JObject jObject)
{
switch ((Node.NodeType)jObject["Type"].Value<int>())
{
case Node.NodeType.Root:
return new Root();
case Node.NodeType.Group:
return new Group();
case Node.NodeType.Category:
return new Category();
case Node.NodeType.Type:
return new Type();
case Node.NodeType.SubType:
return new SubType();
case Node.NodeType.SubsubType:
return new SubSubType();
case Node.NodeType.Item: //I tried this but of course it won't work.
return new Item();
}
return null;
}
}
Any solutions/examples are highly appreciated!!!
Thanks guys.
For those encountering the same issues, I have solved my problem through a combination of different answers out there.
Classes Item and Node have been attributed with the JSON custom converter:
[JsonConverter(typeof(JSONTreeConverter))]
public abstract class Node
[JsonConverter(typeof(JSONTreeConverter))]
public class Item
this enables each class to use the custom converter before initializing.
Then the custom converter returns an object which is cast through reflection to the proper Node or Item type.
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
object targetObj = null;
JObject jo = JObject.Load(reader);
try
{
targetObj = Activator.CreateInstance(objectType); //instantiating concrete and known types
}
catch (Exception exc)
{
switch ((Node.NodeType)jo["Type"].Value<int>())
{
case Node.NodeType.Root:
targetObj = new Root();
break;
case Node.NodeType.Group:
targetObj = new Group();
break;
case Node.NodeType.Category:
targetObj = new ValescoCategory();
break;
case Node.NodeType.Type:
targetObj = new Type();
break;
case Node.NodeType.SubType:
targetObj = new SubType();
break;
case Node.NodeType.SubsubType:
targetObj = new SubSubType();
break;
case Node.NodeType.Item:
targetObj = new Item(); //now this is possible ;)
break;
}
}
foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite))
{
JsonPropertyAttribute att = prop.GetCustomAttributes(true)
.OfType<JsonPropertyAttribute>()
.FirstOrDefault();
string jsonPath = (att != null ? att.PropertyName : prop.Name);
JToken token = jo.SelectToken(jsonPath);
if (token != null && token.Type != JTokenType.Null)
{
object value = token.ToObject(prop.PropertyType, serializer);
prop.SetValue(targetObj, value, null);
}
}
return targetObj;
}
Deserializing as such: JsonConvert.DeserializeObject<Tree>(tree);
That's it! The other overridden function of JSON all return false with Write being not implemented.
Hope this helps someone else because it has taken me 3 days to get to.

C# Json.Net Deserialize Json with different "key" parameter

I am trying to deserialize JSON using the Json.NET library. JSON which I receive looks like:
{
"responseHeader": {
"zkConnected": true,
"status": 0,
"QTime": 2
},
"suggest": {
"mySuggester": {
"Ext": {
"numFound": 10,
"suggestions": [
{
"term": "Extra Community",
"weight": 127,
"payload": ""
},
{
"term": "External Video block",
"weight": 40,
"payload": ""
},
{
"term": "Migrate Extra",
"weight": 9,
"payload": ""
}
]
}
}
}
}
The problem is that the "Ext" that you can see in it is part of the parameter passed in the query string and will always be different. I want to get only the values assigned to the term "term".
I tried something like this, but unfortunately does not works:
public class AutocompleteResultsInfo
{
public AutocompleteResultsInfo()
{
this.Suggest = new Suggest();
}
[JsonProperty("suggest")]
public Suggest Suggest { get; set; }
}
public class Suggest
{
[JsonProperty("mySuggester")]
public MySuggesterElement MySuggesterElement { get; set; }
}
public struct MySuggesterElement
{
public MySuggester MySuggester;
public string JsonString;
public static implicit operator MySuggesterElement(MySuggester MySuggester) =>new MySuggesterElement { MySuggester = MySuggester };
public static implicit operator MySuggesterElement(string String) => new MySuggesterElement { JsonString = String };
}
public class MySuggester
{
[JsonProperty("suggestions")]
public Suggestions[] Suggestions { get; set; }
}
public class Suggestions
{
[JsonProperty("term")]
public string Autocopmplete { get; set; }
}
internal class SuggesterElementConverter : JsonConverter
{
public override bool CanConvert(Type t)
{
return t == typeof(MySuggesterElement) || t == typeof(MySuggesterElement?);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.String:
case JsonToken.Date:
var stringValue = serializer.Deserialize<string>(reader);
return new MySuggesterElement { JsonString = stringValue };
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<MySuggester>(reader);
return new MySuggesterElement { MySuggester = objectValue };
}
throw new Exception("Cannot unmarshal type MySuggesterElement");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (MySuggesterElement)untypedValue;
if (value.JsonString != null)
{
serializer.Serialize(writer, value.JsonString);
return;
}
if (value.MySuggester != null)
{
serializer.Serialize(writer, value.MySuggester);
return;
}
throw new Exception("Cannot marshal type CollationElements");
}
public static readonly SuggesterElementConverter Singleton = new SuggesterElementConverter();
}
public class AutocompleteConverter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
SuggesterElementConverter.Singleton
}
};
}
var results = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(resultJson, AutocompleteConverter.Settings);
Many thanks for your help.
Kind Regerds,
Wojciech
You could decode the "mySuggester" as a dictionary:
public class Suggest
public class Suggest
{
[JsonProperty("mySuggester")]
public Dictionary<string, MySuggester> MySuggester { get; set; }
}
Then you'll be able to access the suggestions with the query string parameter:
var variablePropertyName = "Ext";
var result = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(_json);
var suggestions = result.Suggest.MySuggester[variablePropertyName].Suggestions;
if you don't know the property name you could also look it up in the dictionary:
var variablePropertyName = result.Suggest.MySuggester.Keys.First();
Working example:
https://dotnetfiddle.net/GIKwLs
If you don't need to deserialize the whole json string you can use a JsonTextReader. Example:
private static IEnumerable<string> GetTerms(string json)
{
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("term"))
{
string term = reader.ReadAsString();
yield return term;
}
}
}
}
Using the code:
string json = #"{
""responseHeader"": {
""zkConnected"": true,
""status"": 0,
""QTime"": 2
},
""suggest"": {
""mySuggester"": {
""Ext"": {
""numFound"": 10,
""suggestions"": [
{
""term"": ""Extra Community"",
""weight"": 127,
""payload"": """"
},
{
""term"": ""External Video block"",
""weight"": 40,
""payload"": """"
},
{
""term"": ""Migrate Extra"",
""weight"": 9,
""payload"": """"
}
]
}
}
}
}";
IEnumerable<string> terms = GetTerms(json);
foreach (string term in terms)
{
Console.WriteLine(term);
}
If you only need the object containing the term, and nothing else,
you could work with the JSON values directly by using the JObject interface in JSON.Net.
var parsed = JObject.Parse(jsonString);
var usingLinq = (parsed["suggest"]["mySuggester"] as JObject)
.Descendants()
.OfType<JObject>()
.Where(x => x.ContainsKey("term"));
var usingJsonPath = parsed.SelectTokens("$.suggest.mySuggester.*.*[?(#.term)]")
.Cast<JObject>();

JsonConvert DeserializeObject case sensitive [duplicate]

This question already has an answer here:
Json.NET case-sensitive deserialization
(1 answer)
Closed 6 years ago.
I am trying to deserialize a string content into an object, but I want the content to be case sensitive. The code should only succeed if the string has lower case properties and fail if it has upper case properties. Following is the class:
internal class ResponseList
{
[DataMember]
[JsonProperty]
internal List<Response> Value { get; set; }
}
internal class Response
{
[DataMember]
[JsonProperty]
internal string Id { get; set; }
[DataMember]
[JsonProperty]
internal string Location { get; set; }
[DataMember]
[JsonProperty]
internal PlanClass Plan { get; set; }
}
internal class PlanClass
{
[DataMember]
[JsonProperty]
internal string Name { get; set; }
[DataMember]
[JsonProperty]
internal string Product { get; set; }
[DataMember]
[JsonProperty]
internal string Publisher { get; set; }
}
Following is the code I have. But this is not case-sensitive. It is succeeding for both upper and lowercase:
string content = File.ReadAllText(contentFilePath);
JsonSerializerSettings jsonSerializerSettings1 = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
ResponseList response = (ResponseList)JsonConvert.DeserializeObject(contentResourceOutput, typeof(ResponseList), Constants.JsonSerializerSettings);
The code should only succeed if the content is:
{
"value": [
{
"id": "id1",
"location": "location1",
"plan": {
"name": "free",
"product": "product1",
"publisher": "publisher1"
}
}
]
}
and fail if even if one of the keys is uppercase. E.g.
{
"value": [
{
"Id": "id1",
"Location": "location1",
"plan": {
"Name": "free",
"product": "product1",
"publisher": "publisher1"
}
}
]
}
Notice that only the Keys/Property names should be lower case. The values can be upper case.
Is there a way to make JsonConvert.Deserializeobject case sensitive?
You can write a custom converter to handle this use case. In regards to your need for a recursive inspection of all key names, I used the fantastic WalkNode answer given by Thymine here.
var json = #"{""id"": ""id1"",""name"": ""name1"",""type"": ""type1""}";
var json2 = #"{""id"": ""id1"",""Name"": ""name1"",""type"": ""type1""}";
JsonSerializerSettings settings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter> { new CamelCaseOnlyConverter() }
};
var response = JsonConvert.DeserializeObject<Response>(json, settings);
var response2 = JsonConvert.DeserializeObject<Response>(json2, settings);
public class CamelCaseOnlyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var token = (JObject)JToken.Load(reader);
var isCamelCased = true;
WalkNode(token, null,
t =>
{
var nameFirstChar = t.Name[0].ToString();
if (!nameFirstChar.Equals(nameFirstChar.ToLower(),
StringComparison.CurrentCulture))
{
isCamelCased = false;
return;
}
});
if (!isCamelCased) return null;
return token.ToObject(objectType);
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
JObject o = (JObject)JToken.FromObject(value);
o.WriteTo(writer);
}
private static void WalkNode(JToken node,
Action<JObject> objectAction = null,
Action<JProperty> propertyAction = null)
{
if (node.Type == JTokenType.Object)
{
if (objectAction != null) objectAction((JObject)node);
foreach (JProperty child in node.Children<JProperty>())
{
if (propertyAction != null) propertyAction(child);
WalkNode(child.Value, objectAction, propertyAction);
}
}
else if (node.Type == JTokenType.Array)
foreach (JToken child in node.Children())
WalkNode(child, objectAction, propertyAction);
}
}
The first string will return a hydrated object. The second string will terminate early, returning null.

Creating JSON with dynamic keys in c#

I am trying to replicate the following JSON structure:
{"result_content": {
"data": {
"city_name" : "Beverly Hills",
"2014-06-05T00:00:00": {
"morning_low": "20",
"daytime_high": "40"
},
"2014-06-06T00:00:00": {
"morning_low": "21",
"daytime_high": "41"
},
"2014-06-07T00:00:00": {
"morning_low": "22",
"daytime_high": "42"
},
"2014-06-08T00:00:00": {
"morning_low": "23",
"daytime_high": "43"
},
"2014-06-09T00:00:00": {
"morning_low": "24",
"daytime_high": "44"
}
}
}
}
But I can't figure out how create the keys to be dynamic using C#.
Here are my object class's
public class Day
{
public string morning_low { get; set; }
public string daytime_high { get; set; }
}
public class Data
{
public string city_name { get; set; }
public List<Day> days { get; set; }
}
public class ResultContent
{
public Data data { get; set; }
}
And here is how im building it all:
ResultContent content = new ResultContent();
content.data = new Data();
content.data.city_name = results.Body.GetCityForecastByZIPResponse.GetCityForecastByZIPResult.City;
foreach (Forecast day in results.Body.GetCityForecastByZIPResponse.GetCityForecastByZIPResult.ForecastResult.Forecast){
Day x = new Day();
x.daytime_high = day.Temperatures.DaytimeHigh;
x.morning_low = day.Temperatures.MorningLow;
content.data.days.Add(x);
}
return JsonConvert.SerializeObject(content);
This just returns a JSON array of days which is not what I want. I have the DateTime in my results object.
This is probably what you're looking for in that case...
void Main()
{
Result result = new Result
{
Data = new Data
{
WeatherData = new List<City>
{
new City
{
Name = "London",
Temp = new Dictionary<DateTime, TemperatureRange>
{
{
DateTime.UtcNow,
new TemperatureRange
{
DayHigh = 0,
MorningLow = 50
}
}
}
}
}
}
};
JsonConvert.SerializeObject(result);
}
public class Result
{
[JsonProperty("result_content")]
public Data Data { get; set; }
}
public class Data
{
[JsonProperty("data")]
public List<City> WeatherData { get; set; }
}
public class City
{
[JsonProperty("city_name")]
public string Name { get; set; }
public Dictionary<DateTime, TemperatureRange> Temp { get; set; }
}
public class TemperatureRange
{
public int MorningLow { get; set; }
public int DayHigh { get; set; }
}
If you really want such a sophisticated format, I'd go with a custom JsonConverter:
public class Day
{
public string morning_low { get; set; }
public string daytime_high { get; set; }
}
[JsonConverter(typeof(Data.Converter))]
public class Data
{
public string city_name { get; set; }
public Dictionary<DateTime, Day> days { get; set; }
public class Converter : JsonConverter
{
public override bool CanConvert(Type type) { return type == typeof(Data); }
public override object ReadJson(JsonReader reader, Type type, object value, JsonSerializer serializer)
{
Data obj = new Data();
obj.days = new Dictionary<DateTime, Day>();
DateTime v;
while (reader.Read() && reader.TokenType != JsonToken.EndObject)
{
if (reader.TokenType != JsonToken.PropertyName)
throw new JsonSerializationException("Unexpected token type");
if ("city_name" == (string)reader.Value)
{
if (obj.city_name != null)
throw new JsonSerializationException("Duplicate key: city_name");
obj.city_name = reader.ReadAsString();
}
else if (DateTime.TryParseExact((string)reader.Value, serializer.DateFormatString,
serializer.Culture, DateTimeStyles.None, out v))
{
reader.Read();
obj.days.Add(v, serializer.Deserialize<Day>(reader));
}
else
{
if (serializer.MissingMemberHandling == MissingMemberHandling.Error)
throw new JsonSerializationException("Unexpected property: " + reader.Value);
reader.Read();
serializer.Deserialize(reader, reader.ValueType);
}
}
return obj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Data obj = (Data)value;
writer.WriteStartObject();
writer.WritePropertyName("city_name");
writer.WriteValue(obj.city_name);
foreach (var pair in obj.days)
{
writer.WritePropertyName(pair.Key.ToString(serializer.DateFormatString));
serializer.Serialize(writer, pair.Value);
}
writer.WriteEndObject();
}
}
}
public class ResultContent
{
public Data data { get; set; }
}
public class ResultContentRoot
{
public ResultContent result_content { get; set; }
}
public static void Main()
{
var data = new Data();
data.city_name = "New York";
data.days = new Dictionary<DateTime, Day>();
data.days.Add(DateTime.Today, new Day() { morning_low = "24", daytime_high = "29" });
var result_content = new ResultContent();
result_content.data = data;
var root = new ResultContentRoot();
root.result_content = result_content;
var s = JsonConvert.SerializeObject(root);
}
I think it is the only way to mix dictionary and object contracts.
If you only need one-way serialization, you may also go with dynamic. It takes less code:
public class Day
{
public string morning_low { get; set; }
public string daytime_high { get; set; }
}
public class ResultContent
{
public dynamic data { get; set; }
}
public class ResultContentRoot
{
public ResultContent result_content { get; set; }
}
public static void Main()
{
dynamic data = new ExpandoObject();
data.city_name = "New York";
IDictionary<string, object> days = (IDictionary<string, object>)data;
days.Add(DateTime.Today.ToString("yyyy-MM-dd'T'HH:mm:ss"), new Day() { morning_low = "24", daytime_high = "29" });
var result_content = new ResultContent();
result_content.data = data;
var root = new ResultContentRoot();
root.result_content = result_content;
var s = JsonConvert.SerializeObject(root);
}
But it is very close to discard all that strong typing and just construct response with JObjects.
I think it should be an array of days and representing it the way you asked wouldn't be good, because creating a dynamic json format is difficult to parse.
They way you defined it should produce something like this below.
{
"result_content": {
"data": {
"city_name" : "Beverly Hills",
"days" :
[
{
"morning_low": "20",
"daytime_high": "40"
},
{
"morning_low": "21",
"daytime_high": "41"
},
{
"morning_low": "22",
"daytime_high": "42"
},
{
"morning_low": "23",
"daytime_high": "43"
},
{
"morning_low": "24",
"daytime_high": "44"
}
]
}
}
}
What you're missing is the day itself, which should be defined in your Day class. Add it to get:
"days" :
[
{
"day" : "2014-06-05T00:00:00",
"morning_low": "20",
"daytime_high": "40"
}
...
]

Categories

Resources