'Error converting value' 当 JSON DeserializeObject
'Error converting value' when JSON DeserializeObject
我有一段代码找不到问题所在。我正在使用 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);
这给我错误:
{"Error converting value \"[\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]\" 键入 'System.Collections.Generic.List`1[louisapp.JSONResponse]'。"} System.Exception {Newtonsoft.Json.JsonSerializationException}
我进行了搜索,但找不到问题所在,而且由于我对 JSON 还很陌生,所以我的知识有限。如有任何意见,我们将不胜感激。
编辑:
调用网络的代码 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;
}
}
看起来 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);
List<JSONResponse> rp = jserial.Deserialize<List<JSONResponse>>(returnString);
试试这个对我们来说效果很好
我有一段代码找不到问题所在。我正在使用 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);
这给我错误:
{"Error converting value \"[\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]\" 键入 'System.Collections.Generic.List`1[louisapp.JSONResponse]'。"} System.Exception {Newtonsoft.Json.JsonSerializationException}
我进行了搜索,但找不到问题所在,而且由于我对 JSON 还很陌生,所以我的知识有限。如有任何意见,我们将不胜感激。
编辑: 调用网络的代码 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;
}
}
看起来 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);
List<JSONResponse> rp = jserial.Deserialize<List<JSONResponse>>(returnString);
试试这个对我们来说效果很好