C# Json.NET:带方括号的序列化
如何为我的项目设置标题,如:C# Json.NET:带方括号的序列化,c#,json.net,C#,Json.net,如何为我的项目设置标题,如: {"geometricShapes":[{"circle": "1","triangle": "2","square": "3"},{"circle": "1","triangle": "2","square": "3&
{"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"
}
]
我使用的方法是:
反序列化以将元素添加到模型对象,然后创建一个包含2个副本的列表(geometricShapesList)
string jsonString='{“圆”:“1”,“三角形”:“2”,“正方形”:“3”}'
Model.GeometricShapes GeometricShapes=JsonConvert.DeserializeObject(jsonString);
列表几何图形ShapesList=新列表();
geometricShapesList.Add(geometricShapes);
geometricShapesList.Add(geometricShapes);
字符串jsonStringUpdated=JsonConvert.SerializeObject(geometricShapesList,Formatting.Indented);
Console.WriteLine(jsonStringUpdated);
您可以使用类来解决问题。我建议你采取以下解决办法。您可以使用从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; }
}
}
}
在{…}-之外,您所称的“头”不是有效的JSON。没有引号的
几何图形:
。没有像样的JSON工具可以帮助您生成无效的JSON。有一个属性为列表/数组的对象可以吗?这是微不足道的。检查:SerializeObject(新的{geometricShapes=geometricShapesList})
我想你希望你的JSON看起来像这样:{“geometricShapes”:[{“circle”:“1”,“triangle”:“2”,“square”:“3”}
@Luuk!很抱歉输入错误。所以我只添加到to dovar newJson=new{geometricShapes=geometricShapesList}
,序列化newJson
并打印它。谢谢
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"
}
]
}