25

I am having some problems with create with JSON.Net. When I try to parse it, it gives me following error:

Additional text encountered after finished reading JSON content:

I tried validating it with http://json.parser.online.fr/ and it says "SyntaxError: Unexpected token ,".

My JSON is as below:

{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}

How to deserialize it?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Howard Hee
  • 909
  • 1
  • 13
  • 29

2 Answers2

33

You need to surround that with square brackets, which denotes that it's an array:

    [{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}]
Kevin Schmid
  • 741
  • 5
  • 9
  • 7
    After I tried I get the new error which is:"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'tbl_Staff' because the type requires a JSON object (e.g.{"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array." – Howard Hee May 27 '13 at 03:24
  • 26
    How about just helping him rather than typing all in caps. Sometimes people don't know how to read JSON when they are starting. – Danrex Jan 16 '17 at 20:19
  • I get the same error as @HowardHee comment, so I had to deserialize my code as a "list" of object: `JsonConvert.DeserializeObject>(result);`. It solved the problem. – Ali Majed HA Apr 01 '23 at 19:30
8

As of Release 11.0.1, Json.NET now natively supports parsing comma-delimited JSON in the same way it supports parsing newline delimited JSON:

New feature - Added support for reading multiple comma delimited values with JsonReader.SupportMultipleContent.

Thus the answer to Line delimited json serializing and de-serializing by Yuval Itzchakov should work here also. Define an extension method:

public static partial class JsonExtensions
{
    public static IEnumerable<T> FromDelimitedJson<T>(TextReader reader, JsonSerializerSettings settings = null)
    {
        using (var jsonReader = new JsonTextReader(reader) { CloseInput = false, SupportMultipleContent = true })
        {
            var serializer = JsonSerializer.CreateDefault(settings);

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.Comment)
                    continue;
                yield return serializer.Deserialize<T>(jsonReader);
            }
        }
    }
}

Then, given a data model created to hold an individual item in the comma-separated list such as:

public class RootObject
{
    public string StaffID { get; set; }
    public string StaffRank { get; set; }
}

You can deserialize the JSON string shown like so:

var jsonString = @"{""StaffID"":""S01"",""StaffRank"":""Manager""},{""StaffID"":""S02"",""StaffRank"":""Waiter""}";

var list = JsonExtensions.FromDelimitedJson<RootObject>(new StringReader(jsonString)).ToList();

This approach may be preferable when deserializing a very large sequence of comma-delimited objects from a large file, because it is not necessary to load the entire file into a string then add '[' and ']' to the beginning and end. In Performance Tips: Optimize Memory Usage Newtonsoft recommends deserializing large files directly from a stream, so instead a StreamReader can be passed into JsonExtensions.FromDelimitedJson() which will then stream through the file deserializing each object separately.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
dbc
  • 104,963
  • 20
  • 228
  • 340