I want to serialize/deserialize the following JSON:
{
"result": {
"ID": 1,
"TITLE": "Example",
"ARRAY": [
{
"Item1": "Result1",
"Item2": "Result2"
}
]
}
}
I tried with the following class format, but no sucess yet... Can someone help me deserialize it?
public class myClass
{
public string ID { get; set; }
[JsonProperty("TITLE")]
public string Name { get; set; }
}
obs.: Using the namespace Newtonsoft.JSON
In your example class definition above, you have called the class myClass but you would have had to call it result because ID and TITLE are members of the result JSON in the given example. myClass would not resolve to anything.
I don't know why you'd want to have a property called Name that is mapped to TITLE, but ok, if you want to do that you can modify the solution after you get it working.
Still, we're not done yet. You also have a JSON member called ARRAY and you need to define a separate class for that.
And still there is an additional problem: the result JSON is nested inside an implicit base object, so we need to define that as well. Let's call it BaseResult.
public class ARRAY
{
public string Item1 { get; set; }
public string Item2 { get; set; }
}
public class Result
{
public int ID { get; set; }
public string TITLE { get; set; }
public List<ARRAY> ARRAY { get; set; }
}
public class BaseResult
{
public Result result { get; set; }
}
If you are using Visual Studio, you can copy your JSON and paste it in any *.cs file with Edit > Paste Special > Paste JSON as Classes. It will generate POCO objects representing your JSON, which in your case will be this:
public class Rootobject
{
public Result result { get; set; }
}
public class Result
{
public int ID { get; set; }
public string TITLE { get; set; }
public ARRAY[] ARRAY { get; set; }
}
public class ARRAY
{
public string Item1 { get; set; }
public string Item2 { get; set; }
}
Then, asuming that you have your JSON in a string variable named data, you can deserialize it as follows:
var result= JsonConvert.DeserializeObject<Rootobject>(data);
Related
Modified post to simplify problem:
I have a json object that has the below structure. (data is faked)
I am attempting to deserialize this object using the common Newtonsoft JsonConvert functionality into a class named "Work"
var jWork = JsonConvert.DeserializeObject<Work>(strResponse);
My issue is that if I define the Work class to have public fields as strings, the DeserializeObject will correctly convert the Json values as strings. In other words I can correctly convert the "StringDate" Json field when I have a class defined like below.
However if I define the subfields as classes themselves, the deserialize object no longer works and I get a conversion error.
In other words when I try to do public string DateValue {get ; set} inside of the "Person" class it throw a conversion error.
Why is this?
public partial class Work
{
public Issues Issues { get; set; }
}
public partial class Issues
{
public string Id { get; set; }
public Fields Fields { get; set; }
}
public partial class Fields
{
public string StringDate { get; set; }
public Person Person { get; set; }
public object Customfield11600 { get; set; }
}
public partial class Person
{
public string Name { get; set; }
public string Income { get; set; }
/* I don't want this to be a DateTimeOffset?, i want it to read in the value as string, but if you put string here deserialize throws a conversaion error*/
public DateTimeOffset? Datevalue { get; set; }
/*above works, this doesn't*/
***public string Datevalue { get; set; }***
}
}
Can anyone explain why this is true?
{
"issues" : {
"id": "test",
"fields": {
"StringDate": "2020-02-02",
"person" : {
"name" : "baby",
"Income" : "50000.22",
"datevalue" : "2020-02-02"
},
"customfield_11600": null
}
}
}
I have json file, how can I deserialize this? As I understood json file had array which has 3 elements, but I didn't understand what is inside elements Id, Name, Driver and data inside Driver what is this (Driver) object?
[
{
"Id":1,
"Name":"Renault Magnum",
"Driver":{
"Name":"John",
"Surname":"Dou",
"Age":35,
"Experience":10
},
"State":"base"
},
{
"Id":2,
"Name":"Volvo FH12",
"Driver":{
"Name":"Jack",
"Surname":"Dou",
"Age":55,
"Experience":30
},
"State":"base"
},
{
"Id":3,
"Name":"DAF XF",
"Driver":{
"Name":"Jane",
"Surname":"Dou",
"Age":45,
"Experience":15
},
"State":"base"
}
]
You can use the Paste Special-Feature of Visual Studio to generate classes from a json. By applying this technique to the json above, you get the following classes as a result:
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public int Id { get; set; }
public string Name { get; set; }
public Driver Driver { get; set; }
public string State { get; set; }
}
public class Driver
{
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public int Experience { get; set; }
}
This indicates that the answer to your question what is inside elements Id, Name, Drivder and data inside Driver what is this (Driver) object? is: Yes, it is an object.
I would recommend naming the elements in a useful way though ;-)
I'm trying to deserialize a Json string that has an array with no containing brackets.
{ "id": "983f90j30909j3f",
"moreInfo": {
"info193802": { ... },
"info920938": { ... },
"info849028": { ... }
}
}
This "moreInfo" is an array of items with dynamic keys and does not have square brackets telling that it's an array.
I've tried to deserialize it with Newtonsoft.Json normally ( JsonConvert.DeserializeObject<rootObject>() ) but since this json array isn't really an array it throws an error. Here is my class:
public class RootObject
{
public string Id { get; set; }
public MoreInfo MoreInfo { get; set; }
}
public class MoreInfo
{
public List<Info> InfoList{ get; set; }
}
public class Info
{
properties...
}
How do I go about deserializing this?
Update the root object to use IDictionary<string, Info>
public class RootObject {
public string Id { get; set; }
public IDictionary<string, Info> MoreInfo { get; set; }
}
the dynamic keys will be the key in the dictionary.
Once parsed you access the info via the dictionary's keys
Info info = rootObject.MoreInfo["info193802"];
Newtonsoft can correctly parse the data. The data represents objects, they happen to be nested fairly deep. You can accomplish it a couple of ways, for instance:
dynamic json = JsonConvert.DeserializeObject(response);
var info = json["moreinfo:info913802:example"].Value;
Your other option would be to use Visual Studio, let it create an object you can deserialize to.
Edit
Paste Special
As JSON
Output would be:
public class Rootobject
{
public string id { get; set; }
public Moreinfo moreInfo { get; set; }
}
public class Moreinfo
{
public Info193802 info193802 { get; set; }
public Info920938 info920938 { get; set; }
public Info849028 info849028 { get; set; }
}
public class Info193802
{
public string Example { get; set; }
}
public class Info920938
{
public string Example { get; set; }
}
public class Info849028
{
public string Example { get; set; }
}
The source JSON I used was yours, with one exception:
{ "id": "983f90j30909j3f",
"moreInfo": {
"info193802": { "Example" : "Blah" },
"info920938": { "Example" : "Blah" },
"info849028": {"Example" : "Blah" }
}
}
I want to map this JSON into a .NET class. How can I map this JSON data into a class? Please suggest how. Here is the json:
{"results": [
"43853",
"43855",
"43856",
"43857",
{
"questionType": 3,
"choiceAnswers": [123]
}
]}
The easiest solution is to use Visual Studio Edit > Paste Special > Paste Json As Classes.
But as your json is an array of different objects the .NET class will just be
public class JsonDto
{
public List<object> Results { get; set; }
}
A list of objects will be painful to work with so I recommend that you to use a typed model but then you need to specify you need to define the values, here's an example
{"results": [
{
"key1":"43853",
"key2":"43855",
"key3":"43856",
"key4":"43857",
"question": {
"questionType": 3,
"choiceAnswers": [123]
}
}
]};
public class JsonDto
{
public List<ResultDto> Results { get; set; }
}
public class ResultDto
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Key3 { get; set; }
public string Key4 { get; set; }
public QuestionDto Question { get; set; }
}
public class QuestionDto
{
public int QuestionType { get; set; }
public List<int> ChoiceAnswers { get; set; }
}
You can use online converter to convert json data to c# models http://json2csharp.com For your json it would be something like this.
public class RootObject
{
public List<object> results { get; set; }
}
I have a simple JSON like this:
{
"id": 123,
"name": "BaseName",
"variation": { "name": "VariationName" }
}
Is there a simple way to map it with JSON.NET deserialization to:
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string VariationName { get; set; }
}
I can probably do it with a custom converter, but I hoped there would be a simpler way by annotating the class with attributes which would give instructions to deserialize the variation object just using the one property.
You could set up a class for variation and make VariationName a get-only property
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Variation variation { get; set; }
public string VariationName { get { return variation.VariationName; } }
}
class variation
{
public string name { get; set; }
}