gpt4 book ai didi

c# - 如何使用 JSON.NET 创建具有嵌套值数组的 JSON 字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:18 25 4
gpt4 key购买 nike

感谢@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() 的位置时,我都会收到以下错误:

Token StartObject in state Object would result in an invalid JSON object. Path ''.

有人可以给出一个粗略的代码草稿,说明我如何使用 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();
}
}

关于c# - 如何使用 JSON.NET 创建具有嵌套值数组的 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com