Json.NET :用方括号序列化
Json.NET : Serialization with Square Brackets
我怎样才能为我的项目设置 header:
{"geometricShapes":[{"circle": "1","triangle": "2","square": "3"},{"circle": "1","triangle": "2","square": "3"}]}
我目前的成绩:
[
{
"circle": "1",
"triangle": "2",
"square": "3"
},
{
"circle": "1",
"triangle": "2",
"square": "3"
}
]
我使用的方法:
Deserialize to add the elements to the model object then make a list with 2 copies of it (geometricShapesList)
string jsonString = '{"circle": "1","triangle": "2","square": "3"}'
Model.GeometricShapes geometricShapes = JsonConvert.DeserializeObject<GeometricShapes>(jsonString);
List<GeometricShapes> geometricShapesList = new List<GeometricShapes >();
geometricShapesList.Add(geometricShapes);
geometricShapesList.Add(geometricShapes);
string jsonStringUpdated = JsonConvert.SerializeObject(geometricShapesList, Formatting.Indented);
Console.WriteLine(jsonStringUpdated);
您可以使用类来解决您的问题。我建议您遵循以下解决方案。您可以使用 Json to C# 从 json 字符串生成 类。
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string jsonString = "{\"circle\": \"1\",\"triangle\": \"2\",\"square\": \"3\"}";
Root root = new Root();
GeometricShape geometricShape = JsonConvert.DeserializeObject<GeometricShape>(jsonString);
List<GeometricShape> geometricShapesList = new List<GeometricShape>();
geometricShapesList.Add(geometricShape);
geometricShapesList.Add(geometricShape);
root.geometricShapes = geometricShapesList;
string jsonStringUpdated = JsonConvert.SerializeObject(root, Formatting.Indented);
Console.WriteLine(jsonStringUpdated);
}
public class GeometricShape
{
public string circle { get; set; }
public string triangle { get; set; }
public string square { get; set; }
}
public class Root
{
public List<GeometricShape> geometricShapes { get; set; }
}
}
}
输出:
{
"geometricShapes": [
{
"circle": "1",
"triangle": "2",
"square": "3"
},
{
"circle": "1",
"triangle": "2",
"square": "3"
}
]
}
我怎样才能为我的项目设置 header:
{"geometricShapes":[{"circle": "1","triangle": "2","square": "3"},{"circle": "1","triangle": "2","square": "3"}]}
我目前的成绩:
[
{
"circle": "1",
"triangle": "2",
"square": "3"
},
{
"circle": "1",
"triangle": "2",
"square": "3"
}
]
我使用的方法:
Deserialize to add the elements to the model object then make a list with 2 copies of it (geometricShapesList)
string jsonString = '{"circle": "1","triangle": "2","square": "3"}'
Model.GeometricShapes geometricShapes = JsonConvert.DeserializeObject<GeometricShapes>(jsonString);
List<GeometricShapes> geometricShapesList = new List<GeometricShapes >();
geometricShapesList.Add(geometricShapes);
geometricShapesList.Add(geometricShapes);
string jsonStringUpdated = JsonConvert.SerializeObject(geometricShapesList, Formatting.Indented);
Console.WriteLine(jsonStringUpdated);
您可以使用类来解决您的问题。我建议您遵循以下解决方案。您可以使用 Json to C# 从 json 字符串生成 类。
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string jsonString = "{\"circle\": \"1\",\"triangle\": \"2\",\"square\": \"3\"}";
Root root = new Root();
GeometricShape geometricShape = JsonConvert.DeserializeObject<GeometricShape>(jsonString);
List<GeometricShape> geometricShapesList = new List<GeometricShape>();
geometricShapesList.Add(geometricShape);
geometricShapesList.Add(geometricShape);
root.geometricShapes = geometricShapesList;
string jsonStringUpdated = JsonConvert.SerializeObject(root, Formatting.Indented);
Console.WriteLine(jsonStringUpdated);
}
public class GeometricShape
{
public string circle { get; set; }
public string triangle { get; set; }
public string square { get; set; }
}
public class Root
{
public List<GeometricShape> geometricShapes { get; set; }
}
}
}
输出:
{
"geometricShapes": [
{
"circle": "1",
"triangle": "2",
"square": "3"
},
{
"circle": "1",
"triangle": "2",
"square": "3"
}
]
}