如何使用JSON.NET创建具有嵌套数组值的JSON String?

问题描述 投票:1回答:1

感谢@ 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字符串吗?

c# json nested json.net converters
1个回答
7
投票

如评论中所述,您的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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.