How to build a Json tree - c#

Here's the json I want to build:
{
id: 0,
item: [{ id: 1, text: "1111"
}, {
id: 2,
text: "222222",
item: [{ id: "21", text: "child" }]
}, {
id: 3, text: "3333"
}]
}
For now I can not build a tree, I can only build something like this:
[{"id":1,"text":"1111"},{"id":21,"text":"child"]
I'm building it with this code:
var serializer = new JavaScriptSerializer();
var jsonString = serializer.Serialize(listOfPairsOfTextAndId);
But how can I build the given tree in c#?

Først issue is that it is somewhat difficult to use an anonymous object to describe your graph i C#. But you can create a class instead:
class Item {
public Int32 id { get; set; }
public String text { get; set; }
public Item[] item { get; set; }
}
Then you can create your object graph:
var graph = new Item {
id = 0,
item = new[] {
new Item {
id = 1,
text = "1111"
},
new Item {
id = 2,
text = "222222",
item = new[] {
new Item {
id = 21,
text = "child"
}
}
},
new Item {
id = 3,
text = "3333"
}
}
};
You are using the Microsoft JSON serializer which will result in this JSON:
{"id":0,"text":null,"item":[{"id":1,"text":"1111","item":null},{"id":2,"text":"222222","item":[{"id":21,"text":"child","item":null}]},{"id":3,"text":"3333","item":null}]}
This is not exactly what you want because of the extra null values. To work around this you can use JSON.NET instead. You have to add some extra attributes to the class being serialized:
class Item {
public Int32 id { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public String text { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Item[] item { get; set; }
}
Serialization is then performed using this code:
var jsonString = JsonConvert.SerializeObject(graph);
You get this JSON:
{"id":0,"item":[{"id":1,"text":"1111"},{"id":2,"text":"222222","item":[{"id":21,"text":"child"}]},{"id":3,"text":"3333"}]}
Except for the syntax errors in your question this is what you are asking for.

You need to create some objects that contain the data you want to serialize into that format. I used an online generator to create a couple of quick classes, refactor as necessary for your use.
Here are the classes
public class Item2
{
public string id { get; set; }
public string text { get; set; }
}
public class Item
{
public int id { get; set; }
public string text { get; set; }
public List<Item2> item { get; set; }
}
public class RootObject
{
public int id { get; set; }
public List<Item> item { get; set; }
}
You'll be creating the RootObject first and setting it's properties as necessary (make sure to init the List). After that, simply do the same for the other classes that will make up your output.
After you are done, simple Serialize the RootObject instance and you'll get the output requested.
I used http://json2csharp.com/ to generate those classes based on the desired JSON output.

You can create function like this
public static string GetJSON(object obj)
{
var oSerializer = new JavaScriptSerializer();
var sbJsonResults = new StringBuilder();
oSerializer.Serialize(obj, sbJsonResults);
return sbJsonResults.ToString();
}
And then call this function.
Edit 1
You can create a custom class in which form you want to use.
Create List<class> of that and pass it in the above function
It will work.

Related

Deserializing array of JSONElement back to a concrete list not working when using System.Text.Json

I have a problem when deserializing an object. The object has a property (data) that is a list of JSONElement. I'm doing:
using var doc = JsonDocument.Parse(JsonSerializer.Serialize(result));
var e = doc.RootElement.GetProperty("data");
var data = JsonSerializer.Deserialize<List<MyItem>>(e);
The serialized result variable has the following content:
{
"data":[
{
"id":245,
"number":14,
"name":"Test"
}
],
"totalCount":-1,
"groupCount":-1,
"summary":null
}
And the MyItem class is as follows:
public class MyItem
{
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
}
The data variable is a list with x items. However all items are empty instances.
What am I doing wrong?
The problem is likely that your data is using lowercase property names which are not translated to the property names in your class with the default deserialization settings.
using System.Text.Json;
dynamic result = new
{
data = new dynamic[] {
new {
id = 245,
number = 14,
name = "Test"
}
},
totalCount = -1,
groupCount = -1
};
using var doc = JsonDocument.Parse(JsonSerializer.Serialize(result));
var e = doc.RootElement.GetProperty("data");
List<MyItem> data = JsonSerializer.Deserialize<List<MyItem>>(e);
Console.WriteLine($"{data.First().Id} {data.First().Number} {data.First().Name}");
The above code won't work with your MyItem class, but try this instead:
public class MyItem
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("number")]
public int Number { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
If it works, either use the JsonPropertyName on all your properties or else consider changing your deserialization options.
Everythig can be done in one string of code. You just need to set PropertyNameCaseInsensitive = true of JsonSerializerOptions. Better to make it in a startup file.
List<MyItem> data = JsonDocument.Parse(json).RootElement.GetProperty("data")
.Deserialize<List<MyItem>>();

How to deserialize arrays with reference ids in a JSON using JSON.Net C#

I know that JsonDotNet can deserialize a json taking care of reference ids. But I couldn't find a solution for a simple array with multiple room ids assigned to a tour (in my example). I set my (de)serializer settings to:
var settings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
var data = JsonConvert.DeserializeObject<Data>(json, settings);
My model class looks like:
public class Tour
{
public string Id { get; set; }
public string Name { get; set; }
public List<Room> Rooms { get; set; }
}
public class Room
{
public string Id { get; set; }
public string Name { get; set; }
}
}
And my example json like this:
{
"tours":[
{
"$id":"luw5b23b",
"name":"Tour 1",
"rooms":[
{
"$ref":"k2j3hjh2"
},
{
"$ref":"vg2345fg"
},
{
"$ref":"nb2m5h2j"
}
]
}
],
"rooms":[
{
"$id":"k2j3hjh2",
"name":"Room 1"
},
{
"$id":"vg2345fg",
"name":"Room 2"
},
{
"$id":"nb2m5h2j",
"name":"Room 3"
}
]
}
Unfortunatelly the rooms are null. I'm a totally wrong and this is something I should process as ids and link objects after deserialization by myself or can JsonDotNet do that for me?
Please note, the object ids are provided by a system and should not generated or serialized by JsonDotNet.

C# parsing multiple json

The JSON data is as follows:
{"Sucess":true,
"Code":0,
"Msg":"Sucess",
"Data":{
"UserDayRanking":
{
"UserID":11452112,
"UserCharm":0,
"UserName":"gay",
"UserGender":1,
"UserLevel":36,
"UserPhoto":"http://res.xxx.com/2020/3/16/63719926625601201487545U11452112.jpeg",
"Ranking":0,
"IsNobility":0,
"NobilityType":0,
"NobilityLevel":0,
"UserShowStyle":null,
"LiveLevelUrl":null,
"IsStealth":false},
"DayRankingList":[
{
"UserID":3974854,
"UserCharm":114858,
"UserName":"jack",
"UserGender":1,
"UserLevel":91,
"UserPhoto":"http://res.xxx.com/2020/2/15/63717400601924412312384U3974854.jpeg",
"Ranking":2,
"IsNobility":1,
"NobilityType":1,
"NobilityLevel":3,
"UserShowStyle":
{
"NameColor":100102,
"BorderColor":100403,
"LiangMedal":0,
"DztCountDown":0,
"Mounts":100204,
"LiveLevelCode":0,
"LiveRights":null
},
"LiveLevelUrl":null,
"IsStealth":false
},
{"UserID":6231512,
"UserCharm":22644,
"UserName":"red.girl",
"UserGender":1,
"UserLevel":57,
"UserPhoto":"http://res.xxx.com/2019/11/20/63709843050801519858823U6231512.jpeg",
"Ranking":3,
"IsNobility":0,
"NobilityType":0,
"NobilityLevel":0,
"UserShowStyle":{
"NameColor":0,
"BorderColor":0,
"LiangMedal":0,
"DztCountDown":0,
"Mounts":0,
"LiveLevelCode":0,
"LiveRights":null
},
"LiveLevelUrl":null,
"IsStealth":false}
],
"LiveCharmSwitch":1,
"IsSelf":false
}
}
I want to use c # extraction
"UserID": 3974854,
"UserCharm": 114858,
"UserName": "jack",
"UserID":6231512,
"UserCharm":22644,
"UserName":"red.girl",
That is to extract UserID, UserCharm, UserName,This json has many layers,
What I want after the extraction is,id is sorted in order
id = 1, UserID = 3974854, UserCharm = 114858, UserName = jack
id = 2, UserID = 6231512, UserCharm = 22644, UserName = red.girl
I use the following code, but only extract the first one
string json = #"{"Sucess":true,"Code":0,"Msg":"Sucess","Data":{"UserDayRanking":{"UserID":11452112,"UserCharm":0,"UserName":"gay","UserGender":1,"UserLevel":36,"UserPhoto":"http://res.xxx.com/2020/3/16/63719926625601201487545U11452112.jpeg","Ranking":0,"IsNobility":0,"NobilityType":0,"NobilityLevel":0,"UserShowStyle":null,"LiveLevelUrl":null,"IsStealth":false},"DayRankingList":[{"UserID":3974854,"UserCharm":114858,"UserName":"jack","UserGender":1,"UserLevel":91,"UserPhoto":"http://res.xxx.com/2020/2/15/63717400601924412312384U3974854.jpeg","Ranking":2,"IsNobility":1,"NobilityType":1,"NobilityLevel":3,"UserShowStyle":{"NameColor":100102,"BorderColor":100403,"LiangMedal":0,"DztCountDown":0,"Mounts":100204,"LiveLevelCode":0,"LiveRights":null},"LiveLevelUrl":null,"IsStealth":false},{"UserID":6231512,"UserCharm":22644,"UserName":"red.girl","UserGender":1,"UserLevel":57,"UserPhoto":"http://res.xxx.com/2019/11/20/63709843050801519858823U6231512.jpeg","Ranking":3,"IsNobility":0,"NobilityType":0,"NobilityLevel":0,"UserShowStyle":{"NameColor":0,"BorderColor":0,"LiangMedal":0,"DztCountDown":0,"Mounts":0,"LiveLevelCode":0,"LiveRights":null},"LiveLevelUrl":null,"IsStealth":false}],"LiveCharmSwitch":1,"IsSelf":false}}";
List<Info> jobInfoList = JsonConvert.DeserializeObject<List<Info>>(z);
foreach (Info jobInfo in jobInfoList)
{
//Console.WriteLine("UserName:" + jobInfo.UserName);
}
public class Info
{
public string UserCharm { get; set; }
public string UserName { get; set; }
public data DayRankingList { get; set; }
}
public class data
{
public int UserID { get; set; }
public string UserCharm { get; set; }
public string UserName { get; set; }
public string UserGender { get; set; }
public string UserLevel { get; set; }
}
The above code only shows username = jack,Never show username = red.girl
As it looks to me then you want some details from your JSON has the which is in DayRankingList. As you only want some data then we can use a tool like http://json2csharp.com/ to create our classes and then remove what we don't need. Then we end up with the following classes.
public class DayRankingList
{
public int UserID { get; set; }
public int UserCharm { get; set; }
public string UserName { get; set; }
}
public class Data
{
public List<DayRankingList> DayRankingList { get; set; }
}
public class RootObject
{
public Data Data { get; set; }
}
Which you can deserialise like this
string json = .....
var root = JsonConvert.DeserializeObject<RootObject>(json);
Then if you wish, you can extract the inner data into a new List<> and then just work on that.
List<DayRankingList> rankingLists = root.Data.DayRankingList;
//Do something with this, such as output it
foreach(DayRankingList drl in rankingLists)
{
Console.WriteLine(String.Format("UserId {0} UserCharm {1} UserName {2}",drl.UserId, drl.UserCharm, drl.UserName));
}
You can use Json.Linq to parse your JSON into JObject and enumerate DayRankingList items (since it's an array). Then convert every item into data class and order the result sequence by UserID
var jObject = JObject.Parse(json);
var rankingList = (jObject["Data"] as JObject)?.Property("DayRankingList");
var list = rankingList.Value
.Select(rank => rank.ToObject<data>())
.OrderBy(item => item?.UserID);
foreach (var user in list)
Console.WriteLine($"{user.UserID} {user.UserName}");
Another way is copy your JSON, go to Edit->Paste Special->Paste JSON as classes menu in Visual Studio and generate a proper class hierarchy (I've got 5 classes, they are quite long to post here), then use them during deserialization
The most type-safe way is to define the class structure that you want, like jason.kaisersmith suggested.
To have the final format you need, though, you might want to do an extra Linq Order and Select, to include the id:
var finalList = rankingLists.OrderBy(rl => rl.UserId).Select((value, index) => new
{
id = index,
value.UserId,
value.UserCharm,
value.UserName
});
foreach (var drl in finalList)
{
Console.WriteLine($"Id = {drl.id}, UserId = {drl.UserId}, UserCharm = {drl.UserCharm}, UserName = {drl.UserName}");
}

Converting .Net object to JSON missing most properties

I'm playing around with Web API 4 for the first time and preparing to hook up connection to a MongoDb. I've defined some simple objects to represent the models but when I try to return a collection of them from the GET request of my API only one property is being included in the resulting JSON object.
public class Topic : Entity
{
public string Name { get; set; }
public List<Topic> Parents { get; set; }
public List<Topic> Children { get; set; }
public List<ContentNode> ContentNodes { get; set; }
}
public class ContentNode
{
public enum ContentNodeType { VIDEO, TEXT, AUDIO };
public ContentNodeType ContentType { get; set; }
public string Url { get; set; }
public List<int> Scores { get; set; }
public List<Link> Links { get; set; }
public List<string> Comments { get; set; }
}
public string Get()
{
List<Topic> topics = new List<Topic>();
var programming = new Topic()
{
Id = "1",
Name = "Programming"
};
var inheritanceVideo = new ContentNode()
{
ContentType = ContentNode.ContentNodeType.VIDEO,
Url = "http://youtube.com",
Scores = new List<int>() {
4, 4, 5
},
Comments = new List<string>() {
"Great video about inheritance!"
}
};
var oop = new Topic()
{
Id = "2",
Name = "Object Oriented Programming",
ContentNodes = new List<ContentNode>() {
inheritanceVideo
}
};
programming.Children.Add(oop);
topics.Add(programming);
string test = JsonConvert.SerializeObject(topics);
return test;
}
I'm using the JSON.Net library to serialize the object here but I previously used the default JSON serializer and had the GET return IEnumerable<Topic>. In both cases the JSON being returned is simply:
"[{\"Id\":\"1\"}]"
A browser request for the XML works just fine. According to the documentation for JSON.Net it doesn't seem like there should be a problem serializing these classes to JSON. Any thoughts on why this isn't working? It doesn't seem like I should need to apply explicit attributes for every member.

JavaScriptDeseializer : Can not serialize array

My application is asp.net. I have to send some values back to server. For this I create a object serialize it and send it to server. At server I try to de-serialize it
Following is my code
[Serializable]
public class PassData
{
public PassData()
{
}
public List<testWh> SelectedId { get; set; }
public string SelectedControlClientId { get; set; }
public string GroupTypeId { get; set; }
public string SectionTypeId { get; set; }
}
[Serializable]
public class testWh
{
public testWh()
{
}
public string Id { get; set; }
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
//this can not serialize the SelectedId and the count remains 0
PassData data = serializer.Deserialize<PassData>(jsonString);
//this serialize in an anonymous object with key value pair
var data2 = serializer.DeserializeObject(textHiddenArguments.Text);
Following is my Json Serialized String
{
"SelectedId":{"0":"ABCD","1":"JKLM"},
"SelectedControlClientId":"YTUTOOO",
"GroupTypeId":3,
"SectionTypeId":"1"
}
quotes escaped string
"{\"SelectedId\":{\"0\":\"ABCD\",\"1\":\"JKLM\"},\"SelectedControlClientId\":\"YTUTOOO\",\"GroupTypeId\":3,\"SectionTypeId\":\"1\"}"
My Problem is Selected Id is array of testWH object. But when I try to desrialize it, the SelectedId property of PassData which is list does not get serialized and count remains zero.
I tried using array instead of List, which gave an exception "no parameter less constructor..."
Could any one explain the what I am doing wrong here ?
The key problem here is that the JSON doesn't match the objects you have constructed. You can see this by writing the data you want and serializing:
var obj = new PassData
{
SelectedId = new List<testWh>
{
new testWh { Id = "ABCD"},
new testWh { Id = "JKLM"}
},
GroupTypeId = "3",
SectionTypeId = "1",
SelectedControlClientId = "YTUTOOO"
};
string jsonString = serializer.Serialize(obj);
which gives JSON like:
{"SelectedId":[{"Id":"ABCD"},{"Id":"JKLM"}],
"SelectedControlClientId":"YTUTOOO","GroupTypeId":"3","SectionTypeId":"1"}
So now you need to decide which you want to change; the JSON or the classes. The following alternative class works fine with your original JSON, for example:
public class PassData
{
public Dictionary<string,string> SelectedId { get; set; }
public string SelectedControlClientId { get; set; }
public string GroupTypeId { get; set; }
public string SectionTypeId { get; set; }
}

Categories