如何使用JSON.NET创建具有嵌套值数组的JSON字符串
本文关键字:JSON 数组 嵌套 字符串 何使用 NET 创建 | 更新日期: 2023-09-27 18:22:02
感谢@don jayamanne和@dbc提到我的JSON需要格式良好
这是我重新表述的问题:
我们的应用程序正在使用JSON.NET创建JSON字符串。
以下是我试图创建的JSON字符串:
{
"RouteID": "123321213312",
"DriverName": "JohnDoe",
"Shift": "Night",
"ItineraryCoordinates": [
[
9393,
4443
],
[
8832,
3322
],
[
223,
3432
],
[
223,
3432
]
]
}
以下是我为创建上述JSON字符串而编写的错误代码:
writer.WriteStartObject();
writer.WritePropertyName("RouteID");
serializer.Serialize(writer, routeID);
writer.WritePropertyName("DriverName");
serializer.Serialize(writer, driverName);
writer.WritePropertyName("Shift");
serializer.Serialize(writer, shift);
writer.WritePropertyName("ItineraryCoordinates");
ItineraryCoordinatesCollectionFactory tpCollFac = new ItineraryCoordinatesCollectionFactory();
ItineraryCoordinates anItineraryCoordinates;
StringBuilder coordSB = new StringBuilder();
IList<TimePeriod> ItineraryCoordinatesCollection = tpCollFac.createItineraryCoordinatesCollection();
for (int j = 0; j < ItineraryCoordinatesCollection.Count(); j++)
{
anItineraryCoordinates = ItineraryCoordinatesCollection[j];
writer.WriteStartObject();
writer.WritePropertyName("nested");
coordSB.Append(anItineraryCoordinates.StartTimePeriodCoordinate.X.ToString());
coordSB.Append(" , ");
coordSB.Append(anItineraryCoordinates.StartTimePeriodCoordinate.Y.ToString());
serializer.Serialize(writer, coordSB.ToString());
writer.WriteEndObject();
coordSB.Clear();
writer.WriteStartObject();
writer.WritePropertyName("nested");
coordSB.Append(aTimePeriod.EndTimePeriodCoordinate.X.ToString());
coordSB.Append(" , ");
coordSB.Append(aTimePeriod.EndTimePeriodCoordinate.Y.ToString());
serializer.Serialize(writer, coordSB.ToString());
coordSB.Clear();
writer.WriteEndObject();
} // end of for (int j = 0; j < OrderedTimePeriodsCollection.Count(); j++)
writer.WriteEndObject(); // closing off Json Object LogEventsTimePeriods
每当我更改代码中writer.WriteStartObject()
的位置时,我总是会收到以下错误:
处于Object状态的Token StartObject将导致无效的JSON对象。路径"。
有人能给我一个关于如何使用JSON.NET写出我想要的JSON字符串的粗略代码草案吗?
正如评论中所说,您的JSON无效,可能应该是:
{
"RouteID": "123321213312",
"DriverName": "JohnDoe",
"Shift": "Night",
"ItineraryCoordinates": [
[ 9393, 4443 ],
[ 8832, 3322 ],
[ 223, 3432 ],
[ 223, 3432 ]
]
}
这里有一个示例,它向您展示了构建示例JSON:的两种方法
public class Route
{
public string RouteID { get; set; }
public string DriverName { get; set; }
public string Shift { get; set; }
public int[][] ItineraryCoordinates;
public static string GetSampleJson() {
var sampleRoute = new Route
{
RouteID = "123321213312",
DriverName = "JohnDoe",
Shift = "Night",
ItineraryCoordinates = new int[][] {
new int[] {9393, 4443 },
new int[] { 8832, 3322 },
new int[] { 223, 3432 },
new int[] { 223, 3432 }
}
};
return JsonConvert.SerializeObject(sampleRoute, Formatting.Indented);
}
public static string GetSampleJson2()
{
var route = new JObject(
new JProperty("RouteID", "123321213312"),
new JProperty("DriverName", "JhonDoe"),
new JProperty("Shift", "Night"),
new JProperty("ItineraryCoordinates", new JArray(
new JArray(9393, 4443),
new JArray(8832, 3322 ),
new JArray( 223, 3432 ),
new JArray( 223, 3432)
)
));
return route.ToString();
}
}