如何遍历维基数据JSON对象?

时间:2016-03-06 05:44:50

标签: c# .net json wikidata

我正在尝试迭代一个维基数据JSON对象并且成功到了一定程度。我正在解析以下内容:http://www.wikidata.org/wiki/Special:EntityData/Q319.json

我正在使用以下代码进行迭代:

dynamic a = JsonConvert.DeserializeObject<dynamic>(wikidata_pageContents);
foreach (dynamic entity in a.entities) { 
    foreach (dynamic topLevelStructure in entity.Value) {
    ...

这一直有效,直到我得到如下的“声明”。

{
    "mainsnak": {
        "snaktype": "value",
        "property": "P1036",
        "datavalue": {
            "value": "523.45",
            "type": "string"
        },
        "datatype": "string"
    },
    "type": "statement",
    "id": "Q319$5525e016-4210-bb2c-ce03-bd2dd52cca6c",
    "rank": "normal"
}

我在claim.Value中使用以下JSON文本,但是field_name为null。

foreach (dynamic claimField in claim.Value)
{
    string field_name = claimField.Name;

如何动态迭代上述对象中的值?我宁愿迭代而不是尝试建立一个对象结构并尽可能反序列化。

2 个答案:

答案 0 :(得分:1)

上面代码中的所有内容都是正确的,我们只是不知道你是如何得到claim的,所以我想问题就在那里。这非常简单而且有效:

dynamic a = JsonConvert.DeserializeObject<dynamic>(json);
foreach (dynamic entity in a.entities)
{
    foreach (dynamic topLevelStructure in entity.Value)
    {
        if (topLevelStructure.Name == "claims")
        {
            foreach (dynamic claimField in topLevelStructure.Value)
            {
                var field_name = claimField.Name;
                foreach (var p in claimField.Value)
                {
                    var mainsnak = p.mainsnak;
                    var property = mainsnak.property.Value;
                    var datavalue = mainsnak.datavalue;
                    var value = datavalue.value.Value;
                    var type = datavalue.type.Value;
                }
            }
        }
    }
}

以下是使用Newtonsoft.Json.Linq获取所有声明值的另一种非常简单的方法:

var claims = JObject.Parse(json)["entities"].First.First["claims"];
foreach (var claim in claims.Children())
{
    var mainsnak = claim.First.First["mainsnak"];
    var property = mainsnak["property"];
    var datavalue = mainsnak["datavalue"];
    var value = datavalue["value"];
    var type = datavalue["type"];
}

答案 1 :(得分:0)

您的问题中显示的JSON对象中没有class MyTest extends CustomWebTestCase { public function testSomething() { //Create authoried and unauthorized clients. $authenticatedClient = self::createAuthenticatedClient(array("ROLE_SUPER_ADMIN")); $unauthorizedClient = self::createAuthenticatedClient(array("ROLE_INSUFFICIENT_PERMISSIONS")); // Check if the page behaves properly when the user doesn't have necessary role(s). $unauthorizedClient->request('GET', '/secured-page'); $response = $unauthorizedClient->getResponse(); $this->assertFalse($response->isSuccessful()); $this->assertEquals(403, $response->getStatusCode(), "This request should have failed!"); // Check if the page behaves properly when the user HAS the necessary role(s) $authenticatedClient->request('GET', '/secured-page'); $response = $authenticatedClient->getResponse(); $this->assertTrue($response->isSuccessful()); $this->assertEquals(200, $response->getStatusCode(), "This request should be working!"); } } 字段。所以你宁愿访问Name属性,也可以从那里访问其他属性:

mainsnak