8
votes

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.

4
That JSON is invalid, it has multiple root elements. Try pasting it into jsonformatter.curiousconcept.com and you will see the problem. To read a sequence of JSON root elements, see stackoverflow.com/questions/26601594/… - dbc
I know it has unexpected token at the end, but when I try to deserialize it, it doesn't work :( - Alexander Zaldostanov
Actually, now that I look at your JSON, I realize it has more problems than just being a stream of root objects. For this line: {"messagetype":"chatmsg","msgid":"6478316959_121","from":"sam","message":"this is msg"} ood morning !"}, it appears somebody tried to overwrite a line in the file, and failed to overwrite it completely. Are you sure your JSON is really that bad? - dbc
yes it, I am trying to figure out how to deserialize it to class object, but it keeps throwing me same exception - Alexander Zaldostanov

4 Answers

2
votes

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.

1
votes

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

1
votes

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 ! "}
1
votes

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

Hope this helps someone.