I would like to use newtonsoft json serialzer to create json. I'm totaly new in building LIST and collections, so I will ask you for help. To create a simple json will work. I have to build a attribute list, with a attributelist.
Maybee there is an other libary, making this easier ?
I need this json:
{
"objectTypeId": 545,
"attributes": [{
"objectTypeAttributeId": 222,
"objectAttributeValues": [{
"value": "Kommentar"
}]
}]
}
So for this I startet to
public class Controller
{
public int objectTypeId { get; set; }
public IList<string> attributes { get; set; }
}
Controller controllerjson = new Controller
{
objectTypeId = 545,
// How to add the attributes with each values ?
}
Your c# class model can't match with your JSON data.
There are two way can create model easily.
You can use Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as class, you can easier to know the relation between Json and model.
If you can't use Web Essentials you can instead of use http://json2csharp.com/ online JSON to Model class.
You can try to use those models to carry your JSON Format.
public class ObjectAttributeValue
{
public string value { get; set; }
}
public class Attribute
{
public int objectTypeAttributeId { get; set; }
public List<ObjectAttributeValue> objectAttributeValues { get; set; }
}
public class RootObject
{
public int objectTypeId { get; set; }
public List<Attribute> attributes { get; set; }
}
You can use like this.
RootObject controllerjson = new RootObject()
{
objectTypeId = 545,
attributes = new List<Attribute>() {
new Attribute() {
objectTypeAttributeId = 222,
objectAttributeValues = new List<ObjectAttributeValue>() {
new ObjectAttributeValue() { value= "Kommentar" }
}
}
}
};
string jsonStr = JsonConvert.SerializeObject(controllerjson);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.