我有一个JSON字符串,我正在尝试反序列化到以下类的列表中:
public class Selection
{
public Int64 id { get; set; }
public bool view { get; set; }
public bool edit { get; set; }
public bool broad { get; set; }
}
这是我得到的JSON字符串:
[
[
{"name":"id", "value":0},
{"name":"view", "value":true},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":true},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":true},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
],
[
{"name":"id", "value":0},
{"name":"view", "value":false},
{"name":"edit", "value":false},
{"name":"broad", "value":false}
]
]
这就是我所谓的反序列化:
var selection = JsonConvert.DeserializeObject<List<Selection>>(param.selection);
但是我收到了这个错误:
我需要其他类型的格式吗?
答案 0 :(得分:0)
这个JSON不会轻易解析,你在JSON中将params的名称作为字典的值。 这样做的最好方法是将JSON更改为:
[
{
id :12345,
view:true,
edit : true,
broad :true
},
.....
]
就像这样,它将使用相同的代码进行解析。
如果您无法更改JSON,那么您的代码必须使用反射来设置属性。或者解析为Dictionary并在其上循环,对于每个属性,将其值设置为新对象。
答案 1 :(得分:0)
您收到此错误是因为JSON的形状与您尝试反序列化的结构不匹配。您的JSON表示一组名称 - 值对对象的数组,但您尝试反序列化为List<Selection>
。要反序列化到您定义的类中,您需要使用这样的自定义JsonConverter
:
public class SelectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Selection);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = new JObject(
JArray.Load(reader)
.Children<JObject>()
.Select(jo => new JProperty((string)jo["name"], jo["value"]))
);
var selection = new Selection();
serializer.Populate(obj.CreateReader(), selection);
return selection;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
然后,你可以像这样反序列化:
var selection = JsonConvert.DeserializeObject<List<Selection>>(param.selection, new SelectionConverter());
答案 2 :(得分:-1)
请找到以下解释来序列化j子串。 在我的例子中,我有一个包含学生详细信息的字符串,如下所示。
string json = @"{""data"":[{""id"":""1"",""name"":""A""}, {""id"":""2"",""name"":""B""}, {""id"":""3"",""name"":""C""}, {""id"":""5"",""name"":""D""}]}";
我使用下面的类结构。
public class Students
{
public List<StudentDetails> data { get; set; }
}
public class StudentDetails
{
public string id { get; set; }
public string name { get; set; }
}
使用下面的代码行来序列化json字符串
Students studentdetails = new JavaScriptSerializer().Deserialize<Students>(json);
这里的JavaScriptSerializer()类存在于system.Web.Extensions程序集中。 如果这有帮助,请将此标记为答案。