标签: json-api

JSON API 问题。包含与关系

我在构建 API 端点之前正在阅读这篇文章。我读到了关于复合文档的引用:

为了减少 HTTP 请求的数量,服务器可以允许包含相关资源以及所请求的主要资源的响应。此类回复称为“复合文档”。

以下是使用 JSON API 规范的 JSON 响应示例:

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "links": {
      "self": "http://example.com/articles/1"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": { "type": "people", "id": "9" }
      },
      "comments": {
        "links": {
          "self": "http://example.com/articles/1/relationships/comments",
          "related": "http://example.com/articles/1/comments"
        },
        "data": [
          { "type": "comments", "id": "5" },
          { "type": "comments", "id": "12" }
        ]
      }
    } …
Run Code Online (Sandbox Code Playgroud)

json ruby-on-rails json-api

2
推荐指数
1
解决办法
2695
查看次数

MongoDB 按其他文档中的属性排序

为了扩展我的node.js应用程序的 JSON-API 功能,我尝试根据关系(也称为其他文档)对查询进行排序,尽管我不想返回它们。

根据JSON-API 文档

的排序字段author.name可用于请求根据关系name的属性对主数据进行排序author

例如db.collection('books').find({})返回:

[
    {
        type: "book",
        id: "2349",
        attributes: {
            title: "My Sweet Book"
        },
        relationships: {
            author: {
                data: {
                    type: "authors",
                    id: "9"
                }
            }
        }
    },
    {} // etc ...
]
Run Code Online (Sandbox Code Playgroud)

db.collection('authors').find({id: "9"})返回:

[
    {
        type: "author",
        id: "9",
        attributes: {
            name: "Hank Moody"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

现在我需要某种方法来做类似的事情,例如:
db.collection('books').find({}).sort({"author.name": -1})

我认为我需要将查询转换为聚合,以便我可以使用$lookup运算符,但我不确定如何使用localFieldand foreignField

db.collection('books').aggregate([
    {$match: …
Run Code Online (Sandbox Code Playgroud)

sorting mongodb node.js json-api

2
推荐指数
1
解决办法
2154
查看次数

如何在 JSON:API 中编写两个字类型?

我想将外星文物发布到我的服务器。

我应该写 type="alienArtifact"

或类型=“外星人神器”

或者完全不同的东西?

我在这里查看了https://jsonapi.org/format/但只处理简单类型“对象”。

json-api

2
推荐指数
1
解决办法
1741
查看次数

HATEOAS和链接/动作

我试图围绕如何(以及是否要)在我的api中实现HATEOAS进行研究。我喜欢这样一种概念:仅向客户提供在当前情况下适当的操作。但是我不确定我是否正确实现了这个想法。

假设我有一个资源类型订单,其状态可以更改,它可以具有不同的状态(处理中接受拒绝过期,成功)。然后,我应该创建以下json对象:

{
    ...
    "links": {
        "accept": "http://example.com/order/1/accept",
        "decline": "http://example.com/order/1/decline"
    }
}
Run Code Online (Sandbox Code Playgroud)

还是我在这里创建不必要的动作?如果以上正确,是否应通过PATCH或GET更改状态?如果是PATCH,一个人怎么会知道(打败了超媒体的目的)?

编辑:忘记添加订单ID

rest api-design hateoas hypermedia json-api

1
推荐指数
1
解决办法
1072
查看次数

获取对象JSON Java Android中的所有对象

因此,我试图使用返回JSON响应的英雄联盟API.我没有使用像Jakcson或GSON这样的花哨的lib,

{"type":"champion","version":"5.11.1","data":{"Thresh":{"id":412,"key":"Thresh","name":"Thresh","title":"the Chain Warden"},"Aatrox":{"id":266,"key":"Aatrox","name":"Aatrox","title":"the Darkin Blade"},"Tryndamere":{"id":23,"key":"Tryndamere","name":"Tryndamere","title":"the Barbarian King"},"Gragas":{"id":79,"key":"Gragas","name":"Gragas","title":"the Rabble Rouser"}}}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试访问数据对象中的对象时,我必须明确列出代码中的键名.

关键名称真的很动态,所以它不实用.

有没有办法在没有显式调用键名的情况下获取数据对象中的所有对象?

这是我的Java客户端代码

public String callApi(String API_URL) throws IOException {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(API_URL)
            .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
}

public void buttonClick(View view) throws IOException, JSONException {

    String API_URL = "https://global.api.pvp.net/api/lol/static-data/tr/v1.2/champion?locale=en_US&api_key=my_key_here";

    String champions_json = callApi(API_URL);

    Log.i("summoner",champions_json);

    JSONObject json= new JSONObject(champions_json);
    JSONObject data = json.getJSONObject("data");

    Log.i("summoner", String.valueOf(data));

    List<String> champions = new ArrayList<String>();
    champions.add("Garen");
    champions.add("Aatrox"); …
Run Code Online (Sandbox Code Playgroud)

android json-api

0
推荐指数
1
解决办法
2888
查看次数