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;
...
}
该行:
RootObject RO = (RootObject)jObject;
不正确。如何将DynamicObject分配给我的类?
答案 0 :(得分:1)
您无法将DynamicObject
分配给RootObject
类型的变量,因为类型不是assignable。相反,您应该将JSON反序列化为RootObject
,以便开始使用Json.Decode<T>
:
var RO = System.Web.Helpers.Json.Decode<RootObject>(JsonString);
有关如何反序列化为特定类型的更多示例,另请参阅How can I parse JSON with C#?和How to Convert JSON object to Custom C# object?。