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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With