I am needing to produce this JSON string with C#:
{
"in0": {
"children": [
{
"ValueObjectDescriptor": {
"fields": [
{
"FieldDescriptor": {
"name": "length",
"xpath": "@lenth"
}
},
{
"FieldDescriptor": {
"name": "height",
"xpath": "@height"
}
},
{
"FieldDescriptor": {
"name": "width",
"xpath": "@width"
}
}
],
"objectName": "Job",
"limit": 1,
"xpathFilter": "@openJob = 'true'"
}
}
]
}
}
Here is my code:
static string BuildJsonString()
{
var json = new
{
in0 = new
{
children = new
{
ValueObjectDescriptor = new
{
fields = new
{
FieldDescriptor = new
{
name = "length",
xpath = "@length",
},
FieldDescriptor = new
{
name = "height",
xpath = "@height",
},
FieldDescriptor3 = new
{
name = "width",
xpath = "@width",
},
objectName = "Job",
limit = "1",
xpathFilter = "@openJob='true'"
}
}
}
}
};
var jsonFormatted = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented);
return jsonFormatted.ToString();
The issue I am having is that the compiler doesn't like me using "FieldDescriptor" multiple times, I get the error "An anonymous type cannot have multiple properties with the same name".
I am very new to JSON, so any advice would be greatly appreciated.