[英]C# Assign JSON DynamicObject to a Class
public class RootObject
{
public List<Result> results { get; set; }
public int result_index { get; set; }
}
... ...
private void ReadJson()
{
string JsonString = File.ReadAllText(MyJsonFile);
DynamicObject jObject = System.Web.Helpers.Json.Decode(JsonString);
RootObject RO = (RootObject)jObject;
...
}
The line: 该行:
RootObject RO = (RootObject)jObject;
is not correct. 是不正确的。 How is possible to assign the DynamicObject to my Class?
如何将DynamicObject分配给我的班级?
You cannot assign a DynamicObject
to a variable of type RootObject
because the types are not assignable . 您不能将
DynamicObject
分配给RootObject
类型的变量,因为这些类型不可分配 。 Instead, you should deserialize your JSON as a RootObject
to begin with using Json.Decode<T>
: 相反,您应该使用
Json.Decode<T>
开始将JSON作为RootObject
反序列化:
var RO = System.Web.Helpers.Json.Decode<RootObject>(JsonString);
See also How can I parse JSON with C#? 另请参阅如何使用C#解析JSON? and How to Convert JSON object to Custom C# object?
以及如何将JSON对象转换为自定义C#对象? for more examples of how to deserialize to a specific type.
有关如何反序列化为特定类型的更多示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.