解析JSON从Sitecore的项目的Web API返回 [英] Parsing JSON returned from the Sitecore Item Web API

查看:281
本文介绍了解析JSON从Sitecore的项目的Web API返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从Sitecore的API解析以下JSON对象:

I am having trouble parsing the following JSON object from the Sitecore API:

{
    "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属性 - 它应该是某种字段列表中,而是,它看起来像他们个人的嵌套属性,每个属性的名称是(编号)

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}".

我想要做类似 JsonConvert.DeserializeObject< Sitecore.Data.Items.Item>(jsonString),但我没有成功。我的真正的preFER不具备编写自定义解串器,但它看起来像这就是我唯一的选择。

I wanted to do something like JsonConvert.DeserializeObject<Sitecore.Data.Items.Item>(jsonString), but I have not had success. I would really prefer to not have to write a custom deserializer, but its looking like thats my only option.

即使我想用dyanmic类型的,我能得到尽可能jObject.result.items [0] ...点域,但是然后呢?我不能presume,我将永远知道这些字段的ID。

Even if I wanted to use dyanmic types, I could get as far as jObject.result.items[0].Fields... but then what? I can't presume that I will always know the IDs of these fields.

所以,长话短说,下面是我的问题:

So, long story short, here's my questions:


  1. 有另一种方式与Sitecore的API进行交互,这将有助于我吗?

  2. 解析/反序列化JSON的时候反正周围的IDS-AS-财产的名称问题?

  1. Is there another way to interact with the Sitecore API that would help me?
  2. Is there anyway around the ids-as-property-names problem when parsing/deserializing the JSON?

有关它的价值,这个对象从Sitecore的API,这我从使用这种方法C#应用程序呼唤到来:

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:

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,但也许有人见过这个和周围知道另一种方式?)

(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.Net我做这个如下:

Using JSON.Net I've done this as follows:

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);

(例如,状态code)

现在拿到的项目,你可以做到以下几点:

Now to get item(e.g., statusCode), you can do the following:

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

Console.WriteLine(statusCode);

答:

希望这有助于。

要看到的字段,例如,你可以这样做:

To see Fields,e.g., you can do like this:

JToken desJson = JObject.Parse(json);              

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

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

代替 test.Last.Last的[0]。去年您可以在循环或递归做到这一点。这只是一个例子。

Instead of test.Last.Last[0].Last you can do this in loop or recursion as well. that was just an example.

结果:

这篇关于解析JSON从Sitecore的项目的Web API返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆