简体   繁体   English

用户代码未处理Newtonsoft.Json.JsonSerializationException

[英]Newtonsoft.Json.JsonSerializationException was unhandled by user code

What am I missing here? 我在这里想念什么? Why do I get this exception? 为什么会出现此异常? Newtonsoft.Json.JsonSerializationException was unhandled by user code, Error converting value "[{"username":"someone","computername":"computer1","PID":"1234"}]" to type 'System.Collections.Generic.List`1[WebApplication4.PInfo]'. 用户代码未处理Newtonsoft.Json.JsonSerializationException,将值“ [{” username“:” someone“,” computername“:” computer1“,” PID“:” 1234“}]”“转换为类型'System.Collections时出错。 Generic.List`1 [WebApplication4.PInfo]”。 Path '', line 1, position 95. 路径'',第1行,位置95。

The code is below, very simple class, very simple content, but a nasty error =( 代码在下面,非常简单的类,非常简单的内容,但是令人讨厌的错误=(

public class PInfo
{
    public string username { get; set; }
    public string computername { get; set; }
    public string PID { get; set; }
}

string s = "\"[{\\\"username\\\":\\\"someone\\\",\\\"computername\\\":\\\"computer1\\\",\\\"PID\\\":\\\"1234\\\"}]\"";
var z = JsonConvert.DeserializeObject<List<PInfo>>(s);

I think you have an error in your Json string, the backslashes are maybe incorrect. 我认为您的Json字符串中有错误,反斜杠可能不正确。

If you try this Json string 如果您尝试此Json字符串

[{"username":"test","computername":"test","PID":"test"}]

that you can produce by yourself with the following program then everything works fine: 您可以使用以下程序自行生成,然后一切正常:

private static void test()
    {
        PInfo p = new PInfo();
        p.username = "test";
        p.computername = "test";
        p.PID = "test";
        List<PInfo> testlist = new List<PInfo>();
        testlist.Add(p);
        string json = JsonConvert.SerializeObject(testlist);

        var z = JsonConvert.DeserializeObject<List<PInfo>>(json);
    }

这不是有效的json字符串,请尝试:

string s = "[{\"username\":\"someone\",\"computername\":\"computer1\",\"PID\":\"1234\"}]";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException 错误:Newtonsoft.Json.JsonSerializationException - Error: Newtonsoft.Json.JsonSerializationException Xamarin Newtonsoft.Json.JsonSerializationException: - Xamarin Newtonsoft.Json.JsonSerializationException: Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException问题 - Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException problems 如何处理Newtonsoft.Json.JsonSerializationException? - How to handle Newtonsoft.Json.JsonSerializationException? Newtonsoft.Json.JsonSerializationException: &#39;无法反序列化当前的 JSON 数组 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array Newtonsoft.Json.JsonSerializationException 反序列化 JSON 响应 C# - Newtonsoft.Json.JsonSerializationException in deserializing the JSON response C# Newtonsoft.Json.JsonSerializationException:&#39;无法反序列化当前JSON对象 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object 反序列化JSON对象会引发Newtonsoft.Json.JsonSerializationException - Deserializing JSON object throws a Newtonsoft.Json.JsonSerializationException Newtonsoft.Json.JsonSerializationException:无法反序列化当前的JSON对象 - Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM