我有一些数据是IEnumerable对象,如何将其转换为类类型,类的名称为option
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
class Option
{
public long Id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public long Status { get; set; }
public long Archive { get; set; }
}
到目前为止尝试过=>
foreach (var VARIABLE_3 in (IEnumerable) VARIABLE_2)
{
if ( VARIABLE_3.ToString().Contains("option") && !VARIABLE_3.ToString().Contains("options"))
{
object[] values = ((IEnumerable)VARIABLE_3).Cast<object>().ToArray();
//Option[] values = VARIABLE_3.ToString().Cast<Option>().ToArray();
foreach (var VARIABLE in values)
{
strStringBuilder.Append("VARIABLE: " + VARIABLE + "\n");
}
}
}
答案 0 :(得分:2)
使用JavascriptSerializer
或Json.Net
序列化程序来序列化或反序列化Json字符串。
string jsonArrayObject = @"
[
{ "id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
},
{
"id": 8208,
"name": "Coke",
"price": 0.0,
"status": 1,
"archive": 0
}
]";
JavaScript序列化器
List<Option> optionList = new System.Web.Script.Serialization.
JavaScriptSerializer().Deserialize<List<Option>>(jsonArrayObject);
Json.Net
List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(jsonArrayObject);