我无法从Sitecore API解析以下JSON对象:
{
"statusCode": 200,
"result": {
"totalCount": 1,
"resultCount": 1,
"items": [
{
"Category": "PAGE",
"Database": "web",
"DisplayName": "Profile",
"HasChildren": false,
"Icon": "/temp/IconCache/Network/32x32/earth.png",
"ID": "{7F51AD8B-4A8B-4DA5-87A8-374BEB900801}",
"Language": "en",
"LongID": "/{11111111-1111-1111-1111-111111111111}/{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}/{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}/{D608D67E-01F1-493C-B3B7-23176449CD10}/{7F51AD8B-4A8B-4DA5-87A8-374BEB900801}",
"MediaUrl": "/~/icon/Network/48x48/earth.png.aspx",
"Name": "Profile",
"Path": "/sitecore/content/COMPANY/SITE/PAGE",
"Template": "User Defined/COMPANY/Pages/Base Page",
"TemplateId": "{661AFEB1-8ECE-4D65-80A6-40AC160898D8}",
"TemplateName": "Base Page",
"Url": "~/link.aspx?_id=7F51AD8B4A8B4DA587A8374BEB900801&_z=z",
"Version": 1,
"Fields": {
"{B370A8AA-C7D1-4B79-9BD2-C94675808949}": {
"Name": "Title",
"Type": "Single-Line Text",
"Value": "Profile"
},
"{55373261-F234-444F-9967-E4821C9ACD2C}": {
"Name": "Search Results Text",
"Type": "Multi-Line Text",
"Value": ""
},
"{F7DBD22A-6BE1-4FEE-920C-DF1E688A1224}": {
"Name": "Meta Description",
"Type": "Multi-Line Text",
"Value": "Profile Page"
},
"{BC5B4A35-9526-4DF3-A1EB-3C6A01A52777}": {
"Name": "Tags",
"Type": "Multilist",
"Value": ""
},
"{2AFDEE31-FF47-4184-9BB5-3D17E283F110}": {
"Name": "Enable Meta NoIndex",
"Type": "Checkbox",
"Value": ""
},
"{31D6E245-E63A-4749-90F5-225892F29DB7}": {
"Name": "Enable Meta NoFollow",
"Type": "Checkbox",
"Value": ""
},
"{A79C9551-A6F4-4E9E-9CF4-3DE68C1E9BAB}": {
"Name": "Browser Title",
"Type": "Single-Line Text",
"Value": ""
},
"{4A907846-00BB-4417-BA0E-21B16F5F9875}": {
"Name": "Open Text",
"Type": "Multi-Line Text",
"Value": ""
}
}
}
]
}
}
问题在于Fields属性 - 它应该是某种字段列表,但是看起来它们是单独的嵌套属性,其中每个属性的名称是" {id}&#34 ;
我想做JsonConvert.DeserializeObject<Sitecore.Data.Items.Item>(jsonString)
之类的事,但我没有成功。我真的更喜欢不必编写自定义反序列化程序,但它看起来像我唯一的选择。
即使我想使用dyanmic类型,我也可以获得jObject.result.items [0] .Fields ......但是那么呢?我不能假设我总是知道这些字段的ID。
所以,长话短说,这是我的问题:
对于它的价值,这个对象来自Sitecore API,我使用这种方法从C#应用程序调用它:
var request = (HttpWebRequest)WebRequest.Create(itemUrl);
request.Headers.Add("X-Scitemwebapi-Username", apiUsername);
request.Headers.Add("X-Scitemwebapi-Password", password);
// get response
var response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
(我不认为这对于解决JSON问题非常重要,但也许有人之前已经看过这个并且知道另一种方法吗?)
提前致谢。
答案 0 :(得分:0)
使用JSON.Net我已按如下方式完成此操作:
string json = "{\"statusCode\": 200, \"result\": { \"totalCount\": 1, \"resultCount\": 1, \"items\": [ { \"Category\": \"PAGE\", \"Database\": \"web\", \"DisplayName\": \"Profile\", \"HasChildren\": false, \"Icon\": \"/temp/IconCache/Network/32x32/earth.png\", \"ID\": \"{7F51AD8B-4A8B-4DA5-87A8-374BEB900801}\", \"Language\": \"en\", \"LongID\": \"/{11111111-1111-1111-1111-111111111111}/{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}/{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}/{D608D67E-01F1-493C-B3B7-23176449CD10}/{7F51AD8B-4A8B-4DA5-87A8-374BEB900801}\", \"MediaUrl\": \"/~/icon/Network/48x48/earth.png.aspx\", \"Name\": \"Profile\", \"Path\": \"/sitecore/content/COMPANY/SITE/PAGE\", \"Template\": \"User Defined/COMPANY/Pages/Base Page\", \"TemplateId\": \"{661AFEB1-8ECE-4D65-80A6-40AC160898D8}\", \"TemplateName\": \"Base Page\", \"Url\": \"~/link.aspx?_id=7F51AD8B4A8B4DA587A8374BEB900801&_z=z\", \"Version\": 1, \"Fields\": { \"{B370A8AA-C7D1-4B79-9BD2-C94675808949}\": { \"Name\": \"Title\", \"Type\": \"Single-Line Text\", \"Value\": \"Profile\" }, \"{55373261-F234-444F-9967-E4821C9ACD2C}\": { \"Name\": \"Search Results Text\", \"Type\": \"Multi-Line Text\", \"Value\": \"\" }, \"{F7DBD22A-6BE1-4FEE-920C-DF1E688A1224}\": { \"Name\": \"Meta Description\", \"Type\": \"Multi-Line Text\", \"Value\": \"Profile Page\" }, \"{BC5B4A35-9526-4DF3-A1EB-3C6A01A52777}\": { \"Name\": \"Tags\", \"Type\": \"Multilist\", \"Value\": \"\" }, \"{2AFDEE31-FF47-4184-9BB5-3D17E283F110}\": { \"Name\": \"Enable Meta NoIndex\", \"Type\": \"Checkbox\", \"Value\": \"\" }, \"{31D6E245-E63A-4749-90F5-225892F29DB7}\": { \"Name\": \"Enable Meta NoFollow\", \"Type\": \"Checkbox\", \"Value\": \"\" }, \"{A79C9551-A6F4-4E9E-9CF4-3DE68C1E9BAB}\": { \"Name\": \"Browser Title\", \"Type\": \"Single-Line Text\", \"Value\": \"\" }, \"{4A907846-00BB-4417-BA0E-21B16F5F9875}\": { \"Name\": \"Open Text\", \"Type\": \"Multi-Line Text\", \"Value\": \"\" } } } ] }}";
JToken desJson = JObject.Parse(json);
现在要获取项目(例如,statusCode),您可以执行以下操作:
int statusCode = (int)desJson.SelectToken("statusCode");
Console.WriteLine(statusCode);
答案:
希望这有帮助。
要查看字段,例如,您可以这样做:
JToken desJson = JObject.Parse(json);
var test = desJson.SelectToken("result");
Console.WriteLine(test.Last.Last[0].Last);
您也可以在循环或递归中执行此操作,而不是test.Last.Last[0].Last
。这只是一个例子。
结果: