我是json的新手 - 我正在使用现有的json数据结构并尝试输出数据,但现有数据结构的一部分让我感到难过。
以下是我的json数据:
{"supplier":
{
"supplierid":3590,
"code":"ENCLES",
"name":"Les Miserables",
"analyses":[],
"amenities":[],
"info":
"{\"Supplier\":
{
\"Name\":\"Les Miserables\",
\"LastUpdate\":\"2011-11-01T22:16:06Z\",
\"Address3\":\"London\",
\"Address2\":\"51 Shaftesbury Avenue\",
\"PostCode\":\"W1D 6BA\",
\"Address1\":\"Queen's Theatre\",
\"Address4\":\"\",
\"Address5\":\"\",
\"SupplierId\":3590,
\"SupplierCode\":\"ENCLES\"
}
}",
...
}
让我难倒的是信息数据 - 它是另一个嵌套的json字符串。
我的课程是:
public class TheatreListing
{
public supplier supplier;
}
public class supplier
{
public int? supplierid { get; set; }
public string code { get; set; }
public string name { get; set; }
public listingInfo info { get; set; }
}
public class listingInfo
{
public Address Supplier { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Address4 { get; set; }
public string Address5 { get; set; }
public string PostCode { get; set; }
}
我尝试访问数据的代码是:
TheatreListing tl = Json.Decode<TheatreListing>(json);
StringBuilder sbb = new StringBuilder();
sbb.Append("Name = " + tl.supplier.name.ToString());
sbb.Append("<br />Supplier ID = " + tl.supplier.supplierid.ToString());
sbb.Append("<br />Code = " + tl.supplier.code.ToString());
sbb.Append("<br />Address = " + tl.supplier.info.Supplier.Address2.ToString());
litOutput.Text += sbb.ToString();
我收到的错误消息是:
Cannot convert object of type 'System.String' to type 'listingInfo'
任何人都可以指导我在这里的错误吗?
干杯
佰
答案 0 :(得分:3)
答案 1 :(得分:0)
问题出在
行内TheatreListing tl = Json.Decode<TheatreListing>(json);
我认为您当前的json转换为TheatreListing失败了。
为什么不尝试使用JavascriptSerializer并查看它是否有效。
JavaScriptSerializer js = new JavaScriptSerializer();
TheatreListing tree = js.Deserialize <TheatreListing>(json);