JSON 反序列化对象时“转换值时出错”

问题描述 投票:0回答:4

我有一段代码找不到问题。我正在使用 Visual Studio 2008 并使用 .Net CF 3.5 进行开发。

我收到以下 JSON 字符串:

[
  {
    "id": 1,
    "DbServerInstance": "Ej8/Pz9ubnldPx9ePxk=",
    "DbServerUser": "Px4/Ez8/Pz8/DT9OfBo=",
    "DbServerPassword": "TWsYRiQ/PyM/ZT8/PzJZ",
    "DbServerDatabase": "PwVRczpEPz8aPz8/DnRD",
    "UserManualURL": "Pz8rNUd2PxdAPzM/Uxw/Wg==",
    "ApplicationName": "PzwAAm5MTks/Pz0obD9P"
  }
]

我还创建了以下类:

public class JSONResponse{
    [JsonProperty(PropertyName = "id")]
    public int id { get; set; }

    [JsonProperty(PropertyName = "DbServerInstance")]
    public string dbServerInstance { get; set; }

    [JsonProperty(PropertyName = "DbServerUser")]
    public string dbServerUser { get; set; }

    [JsonProperty(PropertyName = "DbServerPassword")]
    public string dbServerPassword { get; set; }

    [JsonProperty(PropertyName = "DbServerDatabase")]
    public string dbServerDatabase { get; set; }

    [JsonProperty(PropertyName = "UserManualURL")]
    public string userManualURL { get; set; }

    [JsonProperty(PropertyName = "ApplicationName")]
    public string applicationName { get; set; }
}

public class JSONArray
{
    public IList<JSONResponse> Params { get; set; }
}

我尝试使用以下命令进行反序列化:

List<JSONResponse> rp = JsonConvert.DeserializeObject<List<JSONResponse>>(returnString);

这给了我错误:

{“转换值时出错\”[ { \"id\": 1, \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\", \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\", \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\", \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\", \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\", \“应用程序名称\”:\“PzwAAm5MTks / Pz0obD9P \” } ]\" 输入 'System.Collections.Generic.List`1[louisapp.JSONResponse]'。"} System.Exception {Newtonsoft.Json.JsonSerializationException}

我搜索了,但找不到问题,而且由于我对 JSON 很陌生,我的知识有限。任何意见将不胜感激。

编辑: 调用Web api的代码:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "GET";
    request.ContentType = @"application/json";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream);
            return sr.ReadToEnd();
        }
        else
        {
            return null;
        }
    }
c# .net json json.net compact-framework
4个回答
4
投票

看起来

returnString
的 json 格式不正确。查看异常情况,
returnString
在开头和结尾有额外的双引号
\"[
]\"

我尝试了相同的示例,它引发了上述异常

string returnString = "\"[\r\n {\r\n \"id\": 1,\r\n \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\",\r\n \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\",\r\n \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\",\r\n \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\",\r\n \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\",\r\n \"ApplicationName\": \"PzwAAm5MTks/Pz0obD9P\"\r\n }\r\n]\"";

List<JSONResponse> rp = JsonConvert.DeserializeObject<List<JSONResponse>>(returnString);

删除附加引号后,它开始工作

string returnString = "[\r\n {\r\n \"id\": 1,\r\n \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\",\r\n \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\",\r\n \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\",\r\n \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\",\r\n \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\",\r\n \"ApplicationName\": \"PzwAAm5MTks/Pz0obD9P\"\r\n }\r\n]";

List<JSONResponse> rp = JsonConvert.DeserializeObject<List<JSONResponse>>(returnString);

0
投票
List<JSONResponse> rp =    jserial.Deserialize<List<JSONResponse>>(returnString);

试试这个,它对我们来说很好用


0
投票

另外,当您序列化匿名类型时,会发生双重序列化。

var anonymous  = new { Id = 1, Name = "Any Body" };

var jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(anonymous);
return Ok(jsonContent);

Result:  "{\"Id\":1,\"Name\":\"Any Body\"}"

如果直接匿名返回

return Ok(anonymous);
Result: {"id":1,"name":"Any Body"}

0
投票

如果出现匿名类型的双重序列化,您可以使用此示例来避免它:

public object MethodAcceptingObject(object data)
    {
        //var stringData = JsonConvert.SerializeObject(data); //makes to raise your exception
        var request = JsonConvert.DeserializeObject<YourConcreteType>((string)data); //instead of deserializing stringData
...
© www.soinside.com 2019 - 2024. All rights reserved.