没有root对象的C#数据契约 [英] C# data contract without root object
问题描述
我有这样的json输出,我找不到根对象。
[
{
Id:954,
FilmId:ST00000026,
标题:当前战争,
ScreenId:2,
PreShowStartTime:2018-01-25T11:00:00,
},
{
Id:955,
FilmId:ST00000026,
标题:当前战争,
ScreenId:2,
PreShowStartTime:2018-01-25T13:10:00,
}
]
因此尝试通过DataContract将json转换为c#使用此
[DataContract]
公共类RewardRequest1:JsonDataContractObject
{
[DataMember(Name = Id)]
public string Id {get;组; }
[DataMember(Name =FilmId)]
public string FilmId {get;组; }
}
它返回Null值,我找不到根对象。
我尝试了什么:
我试过arry但我无法获得列表。请帮我解决这个问题。
I have json output like this and i cannot find the root object.
[
{
"Id": 954,
"FilmId": "ST00000026",
"Title": "The Current War",
"ScreenId": 2,
"PreShowStartTime": "2018-01-25T11:00:00",
},
{
"Id": 955,
"FilmId": "ST00000026",
"Title": "The Current War",
"ScreenId": 2,
"PreShowStartTime": "2018-01-25T13:10:00",
}
]
So try to convert json to c# through DataContract using this
[DataContract]
public class RewardRequest1 : JsonDataContractObject
{
[DataMember(Name = "Id")]
public string Id { get; set; }
[DataMember(Name = "FilmId")]
public string FilmId { get; set; }
}
It returns Null value and i cannot find the root object.
What I have tried:
I tried arry but i cannot get the list. Please help me to fix this.
推荐答案
你的JSON没有根对象,你只需要创建一个包含2个对象的数组。
如果您要将JSON更改为
Your JSON has no root object, you've just create an array of 2 objects is all.
If you were to change your JSON to
{
"Items":[
{
"Id":954,
"FilmId":"ST00000026",
"Title":"The Current War",
"ScreenId":2,
"PreShowStartTime":"2018-01-25T11:00:00"
},
{
"Id":955,
"FilmId":"ST00000026",
"Title":"The Current War",
"ScreenId":2,
"PreShowStartTime":"2018-01-25T13:10:00"
}
]
}
然后你的根对象将是Items,你的类结构看起来像
Then your root object would be "Items" and your class structure would look like
public class Item
{
public int Id { get; set; }
public string FilmId { get; set; }
public string Title { get; set; }
public int ScreenId { get; set; }
public DateTime PreShowStartTime { get; set; }
}
public class RootObject
{
public List<Item> Items { get; set; }
}
然后你可以使用json.net库做一些像
$ b $这样的事情b
Where you could then, using json.net library, do something like
var mydata = JsonConvert.DeserializeObject<RootObject>(myjsonstring>();
但是在你改变你提供的JSON之前,你没有根对象。
But until you alter your JSON that you've provided, you have no root object.
这篇关于没有root对象的C#数据契约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!