解析Freebase主题HTTP API - JSON和Javascript

时间:2021-07-22 13:13:28

I am trying to parse a JSON output:

我正在解析JSON输出:

http://www.freebase.com/experimental/topic/standard?id=/en/colonel_sanders

http://www.freebase.com/experimental/topic/standard?id=/en/colonel_sanders

I'd like to put the basic data into an array using Javascript. In the "properties" object I'd like to grab any "text" element one level under "properties" as a label and grab the "text" under the "values" object to match the label.

我想使用Javascript将基本数据放入数组中。在“properties”对象中,我希望在“properties”下一级抓取任何“text”元素作为标签,并在“values”对象下抓取“text”以匹配标签。

For the above I would get:

以上我将得到:

  • "description": "Harland David Sanders, better known as Colonel Sanders...
  • “哈兰·大卫·桑德斯上校,更广为人知的名字是桑德斯上校……”
  • "Organizations founded": KFC
  • “组织成立”:肯德基
  • "Cause of death": Leukemia
  • “死因”:白血病
  • "Date of death": Dec 16, 1980
  • “死亡日期”:1980年12月16日
  • "Place of death": Louisville
  • “死亡”的地方:路易斯维尔
  • "Date of birth": Sep 9, 1890
  • “出生日期”:1890年9月9日。
  • "Gender": Male
  • “性别”:男性

etc...

等等……

I have some code which recursively runs through the JSON but I am a novice with javascript and JSON and am having a lot trouble in step one:

我有一些递归运行于JSON的代码,但我对javascript和JSON还不熟悉,在第一步遇到了很多麻烦:

Firstly, grabbing the "text" trying by identifying an element as being "an element of" the main properties object; then

首先,通过识别一个元素作为主要属性对象的“元素”来抓取“文本”;然后

Secondly grabbing from the associated values array any text element (if the value is a collection then I would like to concatenate the strings from the text or otherwise ignore it).

其次,从关联值数组中获取任何文本元素(如果该值是一个集合,那么我希望将文本中的字符串连接起来,或者忽略它)。

I hope that make sense.

我希望这说得通。

nb. the code I use is similar to here: http://tlrobinson.net/projects/javascript-fun/jsondiff/

nb。我使用的代码与这里类似:http://tlrobinson.net/projects/javascript-fun/jsondiff/

1 个解决方案

#1


1  

This should get you started:

这应该让你开始:

<script>
  function cb(response) {
    var props = {};
    var properties = response['/en/colonel_sanders'].result.properties;
    for (var p_id in properties) {
      var prop = properties[p_id];
      props[prop.text]=prop.values[0].text;
    }
    console.log(props);
  }
</script>
<script src="http://www.freebase.com/experimental/topic/standard?id=/en/colonel_sanders&callback=cb"></script>

#1


1  

This should get you started:

这应该让你开始:

<script>
  function cb(response) {
    var props = {};
    var properties = response['/en/colonel_sanders'].result.properties;
    for (var p_id in properties) {
      var prop = properties[p_id];
      props[prop.text]=prop.values[0].text;
    }
    console.log(props);
  }
</script>
<script src="http://www.freebase.com/experimental/topic/standard?id=/en/colonel_sanders&callback=cb"></script>