javascript - 将 JSON 从 API 转换为事件流

标签 javascript angularjs json node.js activity-streams

我正在做一个有趣的项目,其中一个要求是从我选择的 API 中获取 JSON 数据,并使用 angularjs 将其转换为事件流。

我用谷歌搜索并阅读了什么是事件流,我相信我对它有基本的了解。它本质上是使用关键字“actor”、“verb”、“object”和“target”将 Action 转换为特定的 JSON 格式。

当我查看示例时,我理解了为什么特定的 JSON 字符串被视为事件流。

例子:

  {
"published": "2011-02-10T15:04:55Z",
"actor": {
  "url": "http://example.org/martin",
  "objectType" : "person",
  "id": "tag:example.org,2011:martin",
  "image": {
    "url": "http://example.org/martin/image",
    "width": 250,
    "height": 250
  },
  "displayName": "Martin Smith"
},
"verb": "post",
"object" : {
  "url": "http://example.org/blog/2011/02/entry",
  "id": "tag:example.org,2011:abc123/xyz"
},
"target" : {
  "url": "http://example.org/blog/",
  "objectType": "blog",
  "id": "tag:example.org,2011:abc123",
  "displayName": "Martin's Blog"
}

但我对如何将我的 JSON 数据转换为事件流感到困惑。

我的 JSON 没有“ Actor ”,也没有“动词”。如果有人可以解释或提供解决方案,我们将不胜感激。谢谢!

天气 API

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": "Drizzle",
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.32,
    "pressure": 1012,
    "humidity": 81,
    "temp_min": 279.15,
    "temp_max": 281.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 90
  },
  "dt": 1485789600,
  "sys": {
    "type": 1,
    "id": 5091,
    "message": 0.0103,
    "country": "GB",
    "sunrise": 1485762037,
    "sunset": 1485794875
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

最佳答案

ActivityStream 只是事件的规范。天气只是一个模型。模型可以是主体/参与者,也可以是参与者作用于的对象。在这种情况下,天气只能是一个对象。

处理天气的事件示例是:

Martin checks the weather for London at 3:04 PM UTC on February 10, 2015.

对应的事件JSON为:

{
  "published": "2015-02-10T15:04:55Z",
  "actor": {
    "url": "http://example.org/martin",
    "objectType" : "person",
    "id": "tag:example.org,2011:martin",
    "image": {
      "url": "http://example.org/martin/image",
      "width": 250,
      "height": 250
    },
    "displayName": "Martin Smith"
  },
  "verb": "search",
  "object" : {
    "url": "http://api.openweathermap.org/data/2.5/weather?q=London",
    "name": "London's weather",
    "published": "2015-02-10T15:04:55Z"
  }
}

请注意,这只是一个示例。您可能有不同的事件,具体取决于您希望如何使用天气数据。

关于javascript - 将 JSON 从 API 转换为事件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44900776/

相关文章:

javascript - 为什么我的 json_encode 没有在 PHP 中给出正确的值

php - 为什么 JSON_ENCODE 不返回 "/"(斜杠) 它总是像这样返回“\/”?

javascript - 根据自定义排序顺序数组对 JSON 进行排序

javascript - jQuery 到 Javascript 代码转换

javascript - 使用 javascript 将 SVG 保存为 PNG(或其他)

javascript - 如何保存最近100条商品?

angularjs - 如何在 TypeScript 中实现 ng.IDirectiveFactory

javascript - 展平包含对象的数组的数组

javascript - 使用单选按钮控制 Google Geocharts

angularjs - Angular 指令 : using ng-model within isolate scope