I'm trying to set up a series of complex app settings in a separate settings.json file - I won't go into detail as to why...
So I have a JSON file which looks like this:
{
"Website": {
"Name": "Website Name",
"api_key": "----------",
"domain": "-----------"
},
"Pages": {
"Index": {
"Name": "Index",
"Widgets": {
"BestSellers": {
"Name": "BestSellers",
"Type": "ProductCollection",
"Data": {
"Limit": "8",
"Sort": {
"SortType": 3
},
"GetFullProducts": true,
"GroupVariations": false
}
}
}
}
}
}
The first section "Website" simply fetches string settings, all working fine.
The section section "Pages" is more complicated. I have classes that look like this:
public class PageSettings
{
public string Name { get; set; }
public Dictionary<String, Widget> Widgets { get; set; }
public class Widget
{
public string Name { get; set; }
public string Type { get; set; }
public Dictionary<String, object> Data { get; set; } // THIS IS THE PROPERTY THIS QUESTION IS ABOUT
}
}
I use this code to deserialise the above:
IConfigurationSection pagessection = root.GetSection("Pages");
if (pagessection.Exists())
{
_Pages = new Dictionary<String, PageSettings>();
pagessection.Bind(_Pages);
}
With the JSON File exactly as above, this will fail. For some reason, the nested Object Sort in the Data property cannot be deserialised as Object:
"Sort": {
"SortType": 3
}
If I take the above nested object out then all the code so far will work. However, there are use cases where I need that nested object.
I have tried using ExpandoObject which is very cool and clever, but because it expects KeyValuePairs, it then only serialises the nested object in Data, ignoring the simple properties Limit, GetFullroduct etc.
So what I need is a form of ExpandoObject which can also be ExpandoString or something?!
Alternatively... I need to be able to get the Data property from the settings.json file in String form and explicitly deserialise it using JsonConvert.Deserialize at the point of use, because at that point I can declare the proper class that it needs to be deserialised to, but i can't seem to find a way to get the IConfigurationSection code to get the value as a string, rather than a JSON object.
I can't find a solution to this, the nested object breaks everything I have tried.
The helpful comment from #Fei Han has helped a little in highlighting the flexibility of the JObject class, so the solution I have come to is this:
The complex class has to be stored as an HTML encoded string in the settings.json file:
"Data": "{"Limit": "8","GetFullProducts":true,"GroupVariations":true, "Sort": {"SortType": 3}}"
it has to be HTMLEncoded because it is the only way I can find to make the ConfigurationBuilder treat it as a string so that I can cast it correctly later.
The corresponding Class for this now has these properties:
public string ModelString { get; set; }
public Newtonsoft.Json.Linq.JObject Model
{
get
{
string s = ModelString.HtmlDecode();
if (s.EmptyStr())
{
return new JObject();
} else {
return JObject.Parse(s);
}
}
}
From this I am able to easily cast my Data to the eventually required class using .ToObject<MyObject>()
For some reason, this works. I am able to deserialise the string to a JObject in this method, but not directly using the Bind command on IConfigurationSection.
If anyone has any tips on why Bind won't do it, that'd be interesting!
Related
I have this object with a Parent property that reference another object of the same type:
[JsonObject(IsReference = true)]
class Group
{
public string Name { get; set; }
public Group(string name)
{
Name = name;
Children = new List<Group>();
}
public IList<Group> Children { get; set; }
public Group Parent { get; set; }
public void AddChild(Group child)
{
child.Parent = this;
Children.Add(child);
}
}
Serialization works fine and results in json looking like this:
{
"$id": "1",
"Name": "Parent",
"Children": [
{
"$id": "2",
"Name": "Child",
"Children": [],
"Parent": {
"$ref": "1"
}
}
],
"Parent": null
}
But deserialization doesn't work. The Parent property comes back null.
A test looks like this:
[Test]
public void Test()
{
var child = new Group("Child");
var parent = new Group("Parent");
parent.AddChild(child);
var json = JsonConvert.SerializeObject(parent, Formatting.Indented);
Debug.WriteLine(json);
var deserializedParent = (Group) JsonConvert.DeserializeObject(json, typeof(Group));
Assert.IsNotNull(deserializedParent.Children.First().Parent);
}
What am I doing wrong? Any help appreciated!
Using references doesn't work with objects that only have constructors with parameters.
Json.NET has to deserialize all the child values before it creates the parent, it needs those values to pass to the constructor, so there is no valid parent reference to assign to the child.
To expand on James's answer, you can fix this issue by providing a parameterless (default) constructor for Json.Net to use. It can be private if you want, so long as you also mark it with a [JsonConstructor] attribute.
[JsonObject(IsReference = true)]
class Group
{
...
[JsonConstructor]
private Group()
{
}
public Group(string name)
{
Name = name;
Children = new List<Group>();
}
...
}
This arrangement allows Json.Net to create the object without needing all the information up front; it can then use the public properties to fill things in afterward.
Fiddle: https://dotnetfiddle.net/QfqV43
Another way, i found, by being very stubborn is creating a two phase deserialization,
as i describe in the following Deserializing Circular References by Two-Phase deserialization
in general, i create two IContractResolvers, One is used to deserialize only the properties of the Constructor (in case it has parameters).
and in the second phase, I populate the object using regular ContractResolver.
how I can parse following code retrieved in a textbox:
{
"items": [
{
"snippet": {
"channelId": "UCcTbyoZjhqoCn4yVawpMFDA",
"title": "Forever - Stratovarius",
"categoryId": "10"
},
"statistics": {
"viewCount": "6180411",
"likeCount": "19060",
"dislikeCount": "342",
"favoriteCount": "0",
"commentCount": "3025"
}
}
]
}
My code for get value title and likeCount:
Dim url As String = "https://www.googleapis.com/youtube/v3/videos?id=8BvV9arABLs&key=KEYAPI&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics"
Dim json As String = New WebClient().DownloadString(url)
Dim root As JToken = JToken.Parse(json)
Dim sb As New StringBuilder()
For Each item As JToken In root("items")
textbox1.text=sb.AppendLine(item.SelectToken("title") & sb.AppendLine(item.SelectToken("likeCount"))
Next
First, it's better to format marked up data like JSON before you post it.
Like this:
{
"items" : [{
"snippet" : {
"channelId" : "UCcTbyoZjhqoCn4yVawpMFDA",
"title" : "Forever - Stratovarius",
"categoryId" : "10"
},
"statistics" : {
"viewCount" : "6180411",
"likeCount" : "19060",
"dislikeCount" : "342",
"favoriteCount" : "0",
"commentCount" : "3025"
}
}
]
}
As #rufanov said there are a lof of packages for JSON serialization.
As for me, I use Newtonsoft JSON. I use it in C# and will write examples in it, but I strongly believe it should be the same or similar on VB.NET.
Create classes for JSON objects. Pay attention: the members should be names as in JSON object.
public class JItemArray
{
public JItem[] items;
}
public class JItem
{
public JSnippet snippet;
public JStatistics statistics;
}
public class JSnippet
{
public string channelId;
public string title;
public string categoryId;
}
public class JStatistics
{
public string viewCount;
public string likeCount;
public string dislikeCount;
public string favoriteCount;
public string commentCount;
}
Then, you will be able to do
JItemArray itemArray = JsonConvert.DeserializeObject<JItemArray>(yourJsonString);
It will throw JsonReaderException (with explanation in Message) if something fails.
Else, it will return a proper JItem .NET object with all necessary parsed data.
Here it is on NuGet website.
https://www.nuget.org/packages/Newtonsoft.Json/
But if you only need these two values it's pretty good practice to use JToken.
There is many NuGet packages for working with JSON. Use one of them.
I have a view with an HTML table, where the rows are created dynamically.
What is the best way to send the table data to the controller & update the model?
Currently, I have converted the table to JSON, as shown below:
{"0": ["Job Code","Hours","Rate","Expense"],"1": ["a1","b1","c1","d1"],"2": ["a2","b2","c2","d2"]}
I tried to use the newtonsoft JSON deserialize method, but it failed.
I think it failed to convert this format of JSON.
Is there any other method to serialize table that is accepted by newtonsoft JSON?
The {} in JSON tells the de-serializer that it is going to create an object. Keeping this in mind, the JSON you created would make a single object - with each property containing an array of strings. It seems like you want a list of objects, rather than this.
Are you using this method to de-serialize?
JsonConvert.DeserializeObject<T>(json)
You need to provide a type to de-serialize to. So in this case it would be:
class JobDetails
{
public string JobCode { get; set; }
public double Hours { get; set; }
public double Rate { get; set; }
public double Expense { get; set; }
}
In order to de-serialize to this type your JSON.NET JsonConvert call would look like:
[HttpPost]
public ActionResult Testing(string json)
{
var list = JsonConvert.DeserializeObject<List<JobDetails>>(json)
UPDATE_YOUR_MODEL_HERE(list);
return WHAT_YOU_WANT_TO_RETURN_HERE;
}
Your JSON would look like:
[
{
"JobCode": "a1",
"Hours": 37.5,
"Rate": 10,
"Expense": 375
},
{
"JobCode": "a2",
"Hours": 10,
"Rate": 20,
"Expense": 200
},
{
"JobCode": "a3",
"Hours": 37.5,
"Rate": 20,
"Expense": 750
}
]
This will convert the JSON into a List of JobDetails objects.
Alternatively if you want to resolve the JSON to objects as an Action parameter this should do the trick:
[HttpPost]
public ActionResult Testing(List<JobDetails> list)
{
UPDATE_YOUR_MODEL_HERE(list);
return WHAT_YOU_WANT_TO_RETURN_HERE;
}
If you're having trouble getting your table serialized to correct JSON try looking at this jQuery plug-in: https://github.com/lightswitch05/table-to-json
I need to read the following piece of code using RestSharp. My problem is how to get the array in the proper structure. How do I need to setup the class that will contain the object to make this work properly?
I would like to deserialize the object "0" and "1" in a List of type AcUserInfo.
Thanks a lot.
Andrea
{
"0":{
"id":"2",
"subscriberid":"2",
"cdate":"2012-09-28 16:49:06",
"sdate":"2012-09-28 16:49:06",
"first_name":"Al",
"last_name":"",
"email":"test#verizon.net"
},
"1":{
"id":"29",
"subscriberid":"29",
"cdate":"2012-10-02 15:08:29",
"sdate":"2012-10-02 15:08:29",
"first_name":"Mark",
"last_name":"",
"email":"test2#verizon.net"
},
"result_code":1,
"result_message":"Success: Something is returned",
"result_output":"json"
}
Here's the class I've created:
public class SubscriberList {
public int result_code { get; set; }
public string result_message { get; set; }
public string result_output { get; set; }
public List<AcUserInfo> row { get; set; }
SubscriberList(){
row = new List<AcUserInfo>();
}
}
Your JSON data does not contain an array, and therefore cannot be deserialized to a List<>.
Either convert your JSON to something like this:
{
[{
"id":"2",
"subscriberid":"2",
"cdate":"2012-09-28 16:49:06",
"sdate":"2012-09-28 16:49:06",
"first_name":"Al",
"last_name":"",
"email":"test#verizon.net"
},
{
"id":"29",
"subscriberid":"29",
"cdate":"2012-10-02 15:08:29",
"sdate":"2012-10-02 15:08:29",
"first_name":"Mark",
"last_name":"",
"email":"test2#verizon.net"
}],
"result_code":1,
"result_message":"Success: Something is returned",
"result_output":"json"
}
Or if you always only have the "0" & "1" element then change your SubscriberList class to match it.
I have JSON string that has nested objects with dynamic names that vary each time. For an instance:
{
"Objects": {
"dynamicName1": {
"name": "test"
},
"dynamicName2": {
"name": "test"
}
}
}
I was wondering how can you deserialize this string in APEX using wrapper classes?
I tried this:
public class masterobj
{ public childobj Objects;
}
public class childobj
{ public el dynamicName1;
public el dynamicName2;
}
public class el
{ public string name;
}
String s = '{"Objects":{"dynamicName1":{"name":"test"},"dynamicName2":{"name":"test"}}}';
masterobj mo = (masterobj)JSON.deserialize(s, masterobj.class);
which works well when you have declared the dynamic variable names in the class for each nested object.
The problem and the question is how can I make this work using a dynamic variable in the wrapper class. Because the object names will vary and also the number of the objects, I can't hard-code the names as they are different each time.
Any ideas?
You won't be able to deserialize a structure like that with the data binding features of the json parser, you'll need to use the streaming json parser to read it.
Use a Map:
public class masterobj
{
Map<String, el> Objects;
}