2020-12-14 16:52:43,911 DEBUG Newtonsoft.Json.JsonSerializationException: Error converting value "[{"id":1,"aname":"本机域配置","aip":"61.162.59.59","aport":3306,"adb":"cmcp10","auid":"root","apwd":"AYB9dnmtNcY=","amain":2,"astate":2}]" to type 'System.Collections.Generic.List1[Model_v1.dial_area]'. Path '', line 1, position 162. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List
1[Model_v1.dial_area].
在 Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
在 Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
--- 内部异常堆栈跟踪的结尾 ---
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
在 Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
在 Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
以上为错误日志,但确认要解析的字符串应该是没问题,这是什么原因呢.而且方法一直在用,用着用着就出错了
c# 代码
List<dial_area> m_lDialArea1 = JsonConvert.DeserializeObject<List<dial_area>>(Redis2.Instance.Get<string>(Redis2.m_sDialAreaName));
[{
"id": 1,
"aname": "本机域配置",
"aip": "61.162.59.59",
"aport": 3306,
"adb": "cmcp10",
"auid": "root",
"apwd": "AYB9dnmtNcY=",
"amain": 2,
"astate": 2
}]
你这是一个数组,,里面有个json字符串,怎么转成List<T>呢,用jsonArray转
![](https://img2020.cnblogs.com/q/845386/202012/845386-20201216174806909-1267047787.png
确实是这样,能转
你看看。。。但是用着用着就错误了。。。
@穷途之哭灬: 刚试了一下,我在本地调试,是没报错的,你看看报错的json字符串是不是跟你的modle不匹配
@不知道风往哪儿吹:
匹配,一模一样,因为一直用。突然就蹦出来了。反正这个错误,我感觉要么我的字符串就确确实实有问题,因为他提示了位置,正好是最后一个字符的最后一位162那里。要么解析有问题,但是我感觉解析有问题的概率不大
这是你的json:
[
{
"id": 1,
"aname": "本机域配置",
"aip": "61.162.59.59",
"aport": 3306,
"adb": "cmcp10",
"auid": "root",
"apwd": "AYB9dnmtNcY=",
"amain": 2,
"astate": 2
}
]
实体的话是这样:
public class 0
{
public int id { get; set; }
public string aname { get; set; }
public string aip { get; set; }
public int aport { get; set; }
public string adb { get; set; }
public string auid { get; set; }
public string apwd { get; set; }
public int amain { get; set; }
public int astate { get; set; }
}
public class Root
{
public 0 0 { get; set; }
}
json转换成实体需要序列化: string dataJson = JsonConvert.SerializeObject(你的json字符串);
这个是我亲测有效的。