简体   繁体   English

解析从Sitecore Item Web API返回的JSON

[英]Parsing JSON returned from the Sitecore Item Web API

I am having trouble parsing the following JSON object from the Sitecore API: 我无法从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": ""
                    }
                }
            }
        ]
    }
}

The problem is with the Fields property - it should be some sort of list of fields, but instead, it looks like they are individual nested properties where the name of each property is an "{id}". 问题出在Fields属性上-它应该是某种形式的字段列表,但是看起来它们是单独的嵌套属性,其中每个属性的名称均为“ {id}”。

I wanted to do something like JsonConvert.DeserializeObject<Sitecore.Data.Items.Item>(jsonString) , but I have not had success. 我想做类似JsonConvert.DeserializeObject<Sitecore.Data.Items.Item>(jsonString)事情,但是我没有成功。 I would really prefer to not have to write a custom deserializer, but its looking like thats my only option. 真的更喜欢不必编写自定义解串器,但看起来这就是我唯一的选择。

Even if I wanted to use dyanmic types, I could get as far as jObject.result.items[0].Fields... but then what? 即使我想使用动态类型,也可以达到jObject.result.items [0] .Fields ...但那又是什么呢? I can't presume that I will always know the IDs of these fields. 我不能以为我将永远知道这些字段的ID。

So, long story short, here's my questions: 长话短说,这是我的问题:

  1. Is there another way to interact with the Sitecore API that would help me? 还有另一种与Sitecore API交互的方法对我有帮助吗?
  2. Is there anyway around the ids-as-property-names problem when parsing/deserializing the JSON? 解析/反序列化JSON时,ids-as-property-names问题仍然存在吗?

For what it's worth, This object is coming from the Sitecore API, which I am calling out to from a C# application using this approach: 对于它的价值,该对象来自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();

(I don't think that really matters for tackling the JSON problem specifically, but maybe someone has seen this before and knows another way around?) (我认为这对于专门解决JSON问题并不重要,但是也许有人以前见过这种情况,并且知道另一种解决方法吗?)

Thanks in advance. 提前致谢。

Using JSON.Net I've done this as follows: 使用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&amp;_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);

Now to get item(eg, statusCode), you can do the following: 现在要获取项目(例如statusCode),您可以执行以下操作:

int statusCode = (int)desJson.SelectToken("statusCode");

Console.WriteLine(statusCode);

Answer: 回答:

在此处输入图片说明

Hope this helps. 希望这可以帮助。

To see Fields,eg, you can do like this: 要查看Fields,例如,您可以这样做:

JToken desJson = JObject.Parse(json);              

var test = desJson.SelectToken("result");

Console.WriteLine(test.Last.Last[0].Last);

Instead of test.Last.Last[0].Last you can do this in loop or recursion as well. 代替test.Last.Last[0].Last您也可以在循环或递归中执行此操作。 that was just an example. 那只是一个例子。

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM