c# – Newtonsoft.Json.JsonReaderException:读完JSON内容后遇到的其他文本:

我在
JSON数组中遇到一个奇怪的问题,当从服务器接收时,我试图反序列化它,但它说

我创建了一个类,并尝试将其反序列化为该对象,但它说

课程如下.

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

}

例外:Newtonsoft.Json.JsonReaderException:完成读取JSON内容后遇到的其他文本:y.路径”,第1行,第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)
   在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)
   在Listener.Program.LogStatus(布尔接收,Byte []缓冲区,Int32长度)in Listener.Program.d__5.MoveNext()in —从前一个位置抛出异常的堆栈跟踪结束—
   在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
   在System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   在Listener.Program.d__1.MoveNext()

我得到的数组如下,

{"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 ! "}

最后是意外的令牌.

最佳答案 经过大量的工作,我想到了这个:

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);

这将截断其余的字符.

点赞