I have a JSON String as below.
{"Filename":"mypage.html",
"Info":{
"title":{
"Name":"title",
"Values":[
"This is title"
],
"NumericValues":[
],
"DateTimeValues":[
],
"LinkedComponentValues":[
],
"FieldType":0
}
},
"Id":"123",
"Title":"This is my page"
}
I am desearilizing the JSON input string as below.
var outputobject= JsonConvert.DeserializeObject(input);
Now I would like to add a below new node in the input JSON string (after the title in the info) using C#.
"description":{"Name":"description",
"Values":[
"This is description"
],
"NumericValues":[
],
"DateTimeValues":[
],
"LinkedComponentValues":[
],
"FieldType":0
}
After adding the node, I want to searilize the object into string.
I am facing issue to add the new node in the input json string under the Info node. I am just giving small example. But my input JSON string is big one and it has different structure. but it is valid JSON.
My Final JSON should be as below.
{"Filename":"mypage.html",
"Info":{
"title":{
"Name":"title",
"Values":[
"This is title"
],
"NumericValues":[
],
"DateTimeValues":[
],
"LinkedComponentValues":[
],
"FieldType":0
}
"description":{
"Name":"description",
"Values":[
"This is description"
],
"NumericValues":[
],
"DateTimeValues":[
],
"LinkedComponentValues":[
],
"FieldType":0
}
},
"Id":"123",
"Title":"This is my page"
}
This is what am trying, I need to add inside the "Info". Please note, am not creating a model or generating input JSON. I am getting an input JSON as string. After getting JSON string, I need to append a attribute (Description) inside the "Info".
var inputobject = JsonConvert.DeserializeObject(input);var description = new Description
{
Name = "Description",
Values = new List<string>{"This is description"},
NumericValues = new List<string>(),
DateTimeValues = new List<string>(),
LinkedComponentValues = new List<string>(),
FieldType = 0,
KeywordValues = new List<string>(),
};
var descriptionObject = JObject.FromObject(description);
var test = JObject.FromObject(inputobject);
test.Add("Descrition", descriptionObject);
Regards,
Jey
You can use Json.Net's index[] to modify JSON.
class Program
{
static void Main(string[] args)
{
var item = @"
{
""Filename"":""mypage.html"",
""Info"":{
""title"":{
""Name"":""title"",
""Values"":[""This is title""],
""NumericValues"":[],
""DateTimeValues"":[],
""LinkedComponentValues"":[],
""FieldType"":0
}
},
""Id"":""123"",
""Title"":""This is my page""
}";
var description = @"{
""Name"":""description"",
""Values"":[""This is description""],
""NumericValues"":[],
""DateTimeValues"":[],
""LinkedComponentValues"":[],
""FieldType"":0}";
var itemJObj = JObject.Parse(item);
var descriptionJObj = JObject.Parse(description);
var titleJObj = itemJObj["Info"] as JObject;
titleJObj.Add("description", descriptionJObj);
var serializer = new JsonSerializer{ContractResolver = new CamelCasePropertyNamesContractResolver()};
var json = JObject.FromObject(itemJObj, serializer);
Console.WriteLine(json);
Console.ReadLine();
}
}
Always make sure your viewmodels represent what you want to get as the final output.
First Change your classes like below.
public class JsonRoot
{
public string Filename { get; set; }
public Info Info { get; set; }
public string Id { get; set; }
public string Title { get; set; }
}
public class Info
{
public Title title { get; set; }
public Description description { get; set; }
}
public class Title
{
public string Name { get; set; }
public string[] Values { get; set; }
public object[] NumericValues { get; set; }
public object[] DateTimeValues { get; set; }
public object[] LinkedComponentValues { get; set; }
public int FieldType { get; set; }
}
public class Description
{
public string Name { get; set; }
public string[] Values { get; set; }
public object[] NumericValues { get; set; }
public object[] DateTimeValues { get; set; }
public object[] LinkedComponentValues { get; set; }
public int FieldType { get; set; }
}
Then test your json using this test class.
public class MyJsonTest
{
public void TestNewJson()
{
var root = new JsonRoot()
{
Filename = "mypage.html",
Id = "123",
Title = "This is my page",
Info = new Info()
{
description =
new Description()
{
DateTimeValues = new object[] { DateTime.Now, DateTime.UtcNow },
FieldType = 1,
LinkedComponentValues = new object[] { "ABC", "XYZ" },
Name = "MyInfo",
NumericValues = new object[] { 1, 2, 3, 4, 5 },
Values = (new List<string> { "some values" }).ToArray(),
},
title = new Title()
{
FieldType = 5,
NumericValues = new object[] { 1, 2, 43 },
Values = new string[] { "sdfs", "dfgdf" },
Name = "Some name",
LinkedComponentValues = new object[] {"34555", "678786"},
DateTimeValues = new object[] {DateTime.Now},
},
},
};
var json = JsonConvert.SerializeObject(root);
Console.WriteLine(json);
}
This should give you a json like below.
{
"Filename": "mypage.html",
"Info": {
"title": {
"Name": "Some name",
"Values": [
"sdfs",
"dfgdf"
],
"NumericValues": [
1,
2,
43
],
"DateTimeValues": [
"2016-09-28T09:57:52.1513109+10:00"
],
"LinkedComponentValues": [
"34555",
"678786"
],
"FieldType": 5
},
"description": {
"Name": "MyInfo",
"Values": [
"some values"
],
"NumericValues": [
1,
2,
3,
4,
5
],
"DateTimeValues": [
"2016-09-28T09:57:52.1498114+10:00",
"2016-09-27T23:57:52.1508118Z"
],
"LinkedComponentValues": [
"ABC",
"XYZ"
],
"FieldType": 1
}
},
"Id": "123",
"Title": "This is my page"
}