Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content:

18,168

Solution 1

After alot of work, I came up with this:

string final = string.Empty;
string name = encoder.GetString(buffer);
char []arr = name.ToArray();

boolean bln = true;
foreach (char item in arr)
{
    if (bln)
    {
        if (item == '}')
        {
            final += item.ToString();
            break;
        }
        else
        {
            final += item.ToString();
        }
    }
}

Console.WriteLine(final);

which will truncate the rest of the characters.

Solution 2

all of array item keys must placed in your finally class by same name, change your class to this and test it again:

class bundle
{
    public string msgid { get; set; }
    public string messagetype { get; set; }
    public string message { get; set; }
    public string from { get; set; }

}

elso you can convert your JSON array by tools like this: JSON2Csharp

Solution 3

I think your Json is invalid and because you have multiple root element you need to deserialize to List<bundle>.

{"messagetype":"chatmsg","msgid":"123_119","from":"sam","message":"Hi there, good morning ! "}                                                                                                                            
{"messagetype":"chatmsg","msgid":"123_120","from":"sam","message":"how are you?"}                                                                                                                            

{"messagetype":"chatmsg","msgid":"6478316959_121","from":"sam","message":"this is msg. Good morning !"}                                                                                                                            
{"messagetype":"ping","msgid":"6478316959_121","from":"sam","message":"you are crazy morning ! "}

Solution 4

I'm using Newtonsoft; and in my example the serialize worked after I surrounded my entire JSON with these { }

Hope this helps someone.

Share:
18,168
Alexander Zaldostanov
Author by

Alexander Zaldostanov

Updated on June 13, 2022

Comments

  • Alexander Zaldostanov
    Alexander Zaldostanov almost 2 years

    I am facing one strange problem in JSON array while receiving from a server, I tried to deserialize it but it says

    I have created a class and tried to deserialize it into that object but, it says

    the class is given below.

    class bundle
    {
        public string msgid { get; set; }
        public string messagetype { get; set; }
        public string message { get; set; }
        public string from { get; set; }
    
    }
    

    Exception: Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: y. Path '', line 1, position 93. at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value) at Listener.Program.LogStatus(Boolean receiving, Byte[] buffer, Int32 length) in at Listener.Program.d__5.MoveNext() in --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
    at Listener.Program.d__1.MoveNext()

    and the array which I am getting is below,

    {"messagetype":"chatmsg","msgid":"123_119","from":"sam","message":"Hi there, good morning ! "}                                                                                                                            
    {"messagetype":"chatmsg","msgid":"123_120","from":"sam","message":"how are you?"}                                                                                                                            
    
    {"messagetype":"chatmsg","msgid":"6478316959_121","from":"sam","message":"this is msg"} ood morning !"}                                                                                                                            
    {"messagetype":"ping"}g","msgid":"6478316959_121","from":"sam","message":"you are crazy"} orning ! "}
    

    it is unexpected token at the end.

  • Alexander Zaldostanov
    Alexander Zaldostanov about 8 years
    tried it, but doesn't work, the error is still there :(
  • Ali U
    Ali U about 8 years
    do you use JsonConvert.DeserializeObject<bundle>(JsonStr); to parse it? if your array contain 4 items, change it to JsonConvert.DeserializeObject<List<bundle>>(JsonStr); and make your array to valid JSON array => var allItems = [{},{}];
  • Alexander Zaldostanov
    Alexander Zaldostanov about 8 years
    tried it, when I receive the JSON array, it is getting appended with first message from first array, at the end like this. {"messagetype":"chatmsg","msgid":"6478316959_121","from":"sa‌​m","message":"this is msg"} ood morning !"}
  • Suncat2000
    Suncat2000 over 6 years
    That's an object he's trying to deserialize, not an array. He just needs to strip out the extra junk that doesn't belong there.