Thanks to @don-jayamanne and @dbc for mentioned that my JSON needed to be well-formed
Here is my rephrased question:
Our application at work is using JSON.NET to create JSON Strings.
Here is the JSON string that I'm trying to create:
{
"RouteID": "123321213312",
"DriverName": "JohnDoe",
"Shift": "Night",
"ItineraryCoordinates": [
[
9393,
4443
],
[
8832,
3322
],
[
223,
3432
],
[
223,
3432
]
]
}
Here is the faulty code that I wrote to create the aforementioned JSON String:
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
I keep getting the following error whenever I change the location of writer.WriteStartObject() within the code:
Token StartObject in state Object would result in an invalid JSON object. Path ''.
Could someone please give a rough code draft as to how I can write out the JSON String I want using JSON.NET?
"ItineraryCoordinates"to be an array not an object.