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?

0

2 Answers 2

34

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

    [{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}]
3
  • 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<T> 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, 2013 at 3: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, 2017 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<List<Invoice.Root>>(result);. It solved the problem. Apr 1 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.