How to map json and skip parent property? - c#

I have this json string, which contains two elements each with a Number and a Status:
var jsonString = "{\"Errors\":[{\"Number\":9,\"Status\":\"BadRequest\"}, {\"Number\":3,\"Status\":\"BadConnection\"}]}";
As you see it has a parent property called Errors.
I have prepared this model:
public class ExceptionStructure
{
public int Number { get; set; }
public string Status { get; set; }
}
Using NewtonSoft.Json I would like to deserialize the json string into an array of ExceptionStructure objects, without also having to create a model for the parent property (as I don't really need it).
Can I do this (perhaps with some json attribute on the model class)?
I was hoping to do something like this to deserialize:
var exceptionArr = JsonConvert.DeserializeObject<ExceptionStructure>(jsonString);

JSON.NET allows you to deserialize parts of a json file. You can do this by first deserialzing the json string to a JObject, extract the relevant parts, and then deserialize those to your actual object.
JObject errors = JObject.Parse(jsonString);
IList<JToken> results = errors["Errors"].Children().ToList();
IList<ExceptionStructure> exceptions = new List<ExceptionStructure>();
foreach (JToken result in results)
{
ExceptionStructure exception= result.ToObject<ExceptionStructure>();
exceptions.Add(exception);
}
Honestly though, in your case it might be easier to just build a Errors parent class
More information can be found at http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm

this is may be helpful you.
string s = "{\"Errors\":[{\"Number\":9,\"Status\":\"BadRequest\"}, {\"Number\":3,\"Status\":\"BadConnection\"}]}";
var jobj = JObject.Parse(s);
List<ExceptionStructure> list = jobj["Errors"].ToObject<List<ExceptionStructure>>();
OR:
string s = "{\"Errors\":[{\"Number\":9,\"Status\":\"BadRequest\"}, {\"Number\":3,\"Status\":\"BadConnection\"}]}";
List<ExceptionStructure> list = JObject.Parse(s)
.SelectToken("Errors")
.ToObject<List<ExceptionStructure>>();

Related

How to get values from dynamic array?

I have deserialized the string by using the dynamic object.
Because property names dynamically changed. so can't able to deserialize with DTO objects.
Please find the code snippet:
var result = "[{\"series1\":\"{\\\"Category1\\\":4.3,\\\"Category2\\\":2.5,\\\"Category3\\\":3.5}\"},{\"series2\":\"{\\\"Category1\\\":2.4,\\\"Category2\\\":4.4,\\\"Category3\\\":1.8}\"},{\"series3\":\"{\\\"Category1\\\":2,\\\"Category2\\\":2,\\\"Category3\\\":3}\"}]");
var jsonResult = JsonConvert.DeserializeObject<dynamic[]>(jsonText);
How to retrieve the series1 and category1, 2, 3 results?
Could you please help me to solve this?
The problem you will have is you have json stuffed within json - obvious from the fact that you have double escape sequences there. So to get series1 is fairly straightforward:
var jsonResult = JsonConvert.DeserializeObject<dynamic[]>(result);
Console.WriteLine(jsonResult[0]["series1"]);
But that itself is another json string. So you'll need to then parse that again to get Category1 etc:
var jsonResult = JsonConvert.DeserializeObject<dynamic[]>(result);
var series1Result = JsonConvert.DeserializeObject<dynamic>(jsonResult[0]["series1"].ToString());
Console.WriteLine(series1Result["Category1"]); // 4.3
Live example: https://dotnetfiddle.net/hcACrM
As #Jamiec mentioned, there is inline JSON string to consider; so, you will have to deserialize twice.
For this answer, we'll avoid using dynamic altogether (without creating a custom JsonConverter) and avoid JObject (i.e. IDictionary<string, JToken>);
We'll define a Type for Categories. (For the second, iterative Deserialization)
public class Categories
{
public double Category1 { get; set; }
public double Category2 { get; set; }
public double Category3 { get; set; }
}
We'll replace dynamic[] with IEnumerable<IDictionary<string, string>>. Each dictionary has only one entry actually, so we'll simply join the Keys and Values using string.Join() in our iteration handling.
var jsonText = "[{\"series1\":\"{\\\"Category1\\\":4.3,\\\"Category2\\\":2.5,\\\"Category3\\\":3.5}\"},{\"series2\":\"{\\\"Category1\\\":2.4,\\\"Category2\\\":4.4,\\\"Category3\\\":1.8}\"},{\"series3\":\"{\\\"Category1\\\":2,\\\"Category2\\\":2,\\\"Category3\\\":3}\"}]";
var jsonResult = JsonConvert.DeserializeObject<IEnumerable<IDictionary<string, string>>>(jsonText);
var r = jsonResult.Select(i =>
{
(string Series, Categories Categories) result = (string.Join(string.Empty, i.Keys), JsonConvert.DeserializeObject<Categories>(string.Join(string.Empty, i.Values)));
return result;
}).ToArray();
(the last part uses a Deconstruct to ValueTuple)
dynamic here was actually a JObject which implements IDictionary<string, JToken>. JToken is fairly flexible (infact it works with the snippet above - including string.Join usage), but as a matter of scope and preference we deserialized to a primitive string.
Try jsonResult["series1"]["Category"]

How do i deserialize a json to an object? My attemts end up with an JObject and not an object with my properties. How do i avoid this?

I want to convert something like this this {\"ref\":\"/my/path\",\"action\":\"set\",\"payload\":\"\"}
into a generic object in C#. What i tried is this
object mess1 = JObject.Parse(message);
dynamic mess2 = JsonConvert.DeserializeObject<object>(message);
dynamic mess3 = JValue.Parse(message);
The expected result would be an object with the properties ref, action and set. The actual result is an object containing the JObject
ChildrenTokens
Count
First
HasValues
Last
Next
Parent
...
these are not part of my object. What is the correct way of doing this?
The payload in this message is a string OR an arbitrary object. The payload is to be written to a database and i do not care what it contains.
JObject.Parse generates a JObject instance. You can do this a couple of ways, if you already have JObject, you can do the following:
var obj = JObject.Parse(json);
var command = obj.ToObject<Command>();
Or, you can work from the string:
var command = JsonConvert.Deserialize<Command>(json);
We generally use
this method
public static JObject JsonParsed(string Json)
{
return JObject.Parse(Json);
}
You can deserialize it into a dynamic object with:
dynamic parsed = JObject.Parse("ref\":\"/my/path\",\"action\":\"set\",\"payload\":\"\");
And access your properties like any other object:
Console.WriteLine(parsed.ref); // "/my/path/"
Console.WriteLine(parsed.action); // "set"
Console.WriteLine(parsed.payload); // "\"
By "object with my properties" you mean what's often referred as POCO.
You have to define a POCO object like so:
public class Poco
{
public string Ref { get; set; }
public string Action { get; set; }
public string Payload { get; set; }
}
Then deserialize your JSON like so:
string json = "{'ref':'/my/path','action':'set','payload':''}";
Poco myPoco = JsonConvert.DeserializeObject<Poco>(json);
Now you will have myPoco.Ref, myPoco.Action, myPoco.Payload properties populated from your JSON.

How to get object json when deserializing array

I have an incoming JSON, which consists array of some objects, say, Foo. I deserialize them with
result = JsonConvert.DeserializeObject<List<Foo>>(message);
Now i want to add a string property to Foo, which will store it's JSON (which i received), so that Foo'll look like:
public class Foo
{
public int MyInt { get; set; }
public bool MyBool { get; set; }
public string JSON { get; set; }
}
But i don't know how can i say JSON.Net the way it can populate such a field..
UPD
I'll clearify what i want. Say i receive JSON:
[{"MyInt":1,"MyBool":0},{"MyInt":2,"MyBool":0},{"MyInt":3,"MyBool":1}]
Here is array of 3 objects and i want, when deserializing, to add corresponding part of json to object, so that:
First object will contain {"MyInt":1,"MyBool":0}
Second object will contain {"MyInt":2,"MyBool":0}
Third object will contain {"MyInt":3,"MyBool":1}
in their JSON Property
I'll be gratefull for any help!
This is one way to do it, but it doesn't maintain the exact original JSON - but it does provide a static record of the original JSON (but without the exact format of the original values - i.e. Bool maybe be 0/1 or true/false):
var message = #"[{""MyInt"":1,""MyBool"":0},{""MyInt"":2,""MyBool"":0},{""MyInt"":3,""MyBool"":1}]";
var foos = JsonConvert.DeserializeObject<List<Foo>>(message);
var t = JsonConvert.SerializeObject(foos[0]);
foos = foos.Select(s => new Foo() { MyBool = s.MyBool, MyInt = s.MyInt, JSON = JsonConvert.SerializeObject(s) }).ToList();
If you are dealing with a lot of Foos, then you might want to find a more efficient way. There might be a way to 'update' using linq, rather than creating a new list.
Okay, i found an answer. I didn't know that i can deserialize message into JArray and then enumerate it (good job, newtonsoft:) ). Here is what i endede up with:
if (tokenType is JArray)
{
var arr = JsonConvert.DeserializeObject(message) as JArray;
foreach (var item in arr)
{
try
{
var agentParameter = item.ToObject<Foo>();
agentParameter.JSON = item.ToString();
result.Add(agentParameter);
}
catch (Exception)
{
LogProvider.Error(string.Format("Failed to Deserialize message. Message text: \r\n {0}", item.ToString()));
}
}
}

How to parse non-array JSON?

I am trying to read json from a local .json file and parse the contents using StreamReader and Json.NET. Json & my code:
contents of .json file: {"rate":50,"information":{"height":70,"ssn":43,"name":"andrew"}}
using (var sr = new StreamReader(pathToJsonFile))
{
dynamic jsonArray = JsonConvert.DeserializeObject(sr.ReadToEnd());
foreach(var item in jsonArray)
{
Console.WriteLine(item.rate);
Console.WriteLine(item.ssn);
}
}
This gives me an error on the line foreach(var item in array): Object reference not set to an instance of an object. I am guessing this is because my json is not actually an array but that is how I am trying to parse it. How can I parse this json in order to pull out fields such as rate or ssn?
NB - please do not flag this question as a duplicate of Read and parse a Json File in C#, as that is where I got my original code from.
EDIT: As has been pointed out in other answers, jsonArray is null. That explains my error but still does not answer my question. How else can I parse this json in order to extract the desired fields?
A couple things:
If you want to manually parse out the values, you should try using JObject rather than JsonConvert.DeserializeObject. The following code should work:
dynamic jsonObject = JObject.Parse("{'rate':50,'information':{'height':70,'ssn':43,'name':'andrew'}}");
Console.WriteLine(jsonObject["rate"]);
Console.WriteLine(jsonObject["information"]["ssn"]);
However, if you know how the json is structured, you should create a .net class like:
public class Person
{
public int rate {get;set;}
public Information information {get;set;}
}
public class Information
{
public int height {get;set;}
public int ssn {get;set;}
public string name {get;set;}
}
and then use:
var person = JsonConvert.DeserializeObject<Person>(thestringtodeserialize);
That way you can have a strongly typed object.
In any case, I would check for null (DeserializeObject can obviously return null):
using (var sr = new StreamReader(pathToJsonFile))
{
dynamic jsonArray = JsonConvert.DeserializeObject(sr.ReadToEnd());
if(jsonArray != null) //new check here
{
foreach(var item in jsonArray)
{
Console.WriteLine(item.rate);
Console.WriteLine(item.ssn);
}
}
I am guessing this is because my json is not actually an array
True, the returned object is dynamic, so make use of dynamic:
var json = "{\"rate\":50,\"information\":{\"height\":70,\"ssn\":43,\"name\":\"andrew\"}}";
dynamic obj = JsonConvert.DeserializeObject(json);
Console.WriteLine("rate: {0}. ssn: {1}", obj.rate, obj.information.ssn);
See live sample here: https://dotnetfiddle.net/nQYuyX
Are you sure it's an array?
If that's the format the you expect from Json, maybe you should consider defining a class.
For example:
class SomeJsonObject
{
public int rate {get;set;}
[JsonProperty("information")] //if you want to name your property something else
public InformationObject Information {get;set;}
}
class InformationObject
{
[JsonProperty("height", NullValueHandling = NullValueHandling.Ignore)] //some other things you can do with Json
public int Height {get;set;}
public int ssn {get;set;}
public string name {get;set;}
}
This way you can just deserialize it to an object:
SomeJsonObject jsonArray = JsonConvert.DeserializeObject<SomeJsonObject>(sr.ReadToEnd());
I think your question is similar to this Deserialize JSON with C# . you can use JavaScriptSerializer
I don't get a null reference (with Json.net 6.0.3) but your code has one obvious bug:
static void Main(string[] args)
{
string s = "{'rate':50,'information':{'height':70,'ssn':43,'name':'andrew'}}".Replace('\'', '\"');
var obj = JsonConvert.DeserializeObject(s);
dynamic jsonArray = obj;
foreach (var item in jsonArray)
{
Console.WriteLine(item.rate);
Console.WriteLine(item.ssn);
}
}
The bug is Console.WriteLine(item.rate) will throw.
Your 'array' jsonArray is not actually an array, it is a dictionary!
Therefore, item=the first Key-Value-pair in the dictionary, = {"rate":50}.
You can prevent the code from throwing by getting rid of your foreach loop.
i would fire up nuget and get the JSON.net package
https://www.nuget.org/packages/Newtonsoft.Json/
http://james.newtonking.com/json
it is well documented and can save you a tonne of work.
see also http://json2csharp.com/
EDIT: you are already using this

How to map a data contract to refer to correct fields in a JSON object?

I'm recieving a JSON object that looks like the example below.
{
"name1":{"name1a":"value1a","name1b":"value1b"},
"name2":{"name2a":"value2a","name2b":"value2b"}
}
I've set up a data contract for it (since I only need to access a single data field at the moment) like this.
[DataContract]
public class MyThingy
{
[DataMember(Name="name1b")]
public string Name1b { get; set; }
public MyThingy() { }
public MyThingy(String name1b)
{
Name1b = name1b;
}
}
When I've serialized the object, I try to print it out (which works, since I'm getting a string description of the class) and them the field Name1b. The last part doesn't work and I'm getting null there. My guess is that I must have mapped the data contract wrongly but I can't see how to correct it.
How should the MyThingy class be declared?
My JSON object is fetched as described in this post.
I would use JavaScriptSerializer here,
string json = #"{
""name1"":{""name1a"":""value1a"",""name1b"":""value1b""},
""name2"":{""name2a"":""value2a"",""name2b"":""value2b""}
}";
var obj = new JavaScriptSerializer()
.Deserialize<Dictionary<string, Dictionary<string, string>>>(json);
Console.WriteLine(obj["name1"]["name1b"]);
You can also use Json.Net and dynamic together
dynamic obj = JsonConvert.DeserializeObject(json);
Console.WriteLine(obj.name1.name1b);

Categories