我遵循JSON,我使用Json Serializer序列化以从JSON读取值。
JSON
AccessTypes: {
AccessType: [
{
Services: "",
Type: "VDSL",
ProviderId: 1711339,
ServiceDetailExtData: "",
Serviceability: "Serviceable",
Provider: "LFC Chorus",
Priority: 500
},
{
Services: "",
Type: "UBA",
ProviderId: 1711339,
ServiceDetailExtData: "",
Serviceability: "Serviceable",
Provider: "LFC Chorus",
Priority: 500
}
]
}
我正在使用以下课程
public class AccessType
{
public string Type { get; set; }
public string ProviderId { get; set; }
public string Serviceability { get; set; }
public string Provider { get; set; }
public string Priority { get; set; }
public string IntactStatus { get; set; }
}
public class AccessTypes
{
public List<AccessType> AccessType { get; set; }
}
但是有时我的客户端发送的JSON不是像下面那样只有单个值的数组或列表。
AccessTypes: {
AccessType:
{
Services: "",
Type: "VDSL",
ProviderId: 1711339,
ServiceDetailExtData: "",
Serviceability: "Serviceable",
Provider: "LFC Chorus",
Priority: 500
}
}
您能帮忙解决如何处理可以列表或单值的标签。我能想到的一种方法是用&#34; [{&#34;获得一个列表,但必须有更好的方法来做到这一点。
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(AccessTypes));
Stream str = response.GetResponseStream();
object objResponse = jsonSerializer.ReadObject(str);