如何从api获取以下数据?

p8ekf7hl  于 2021-09-13  发布在  Java
关注(0)|答案(3)|浏览(331)

关闭。这个问题需要详细或明确。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,添加细节并澄清问题。

五小时前关门了。
改进这个问题
我想得到word、string1和string2。对不起,我的英语不好,谢谢你的回答。
这是我的代码:

Item item = new Item(jsonObject.getString("word"), 
                     jsonObject.getString("string1"), jsonObject.getString("string2"));

这是api提供的数据:

[{"word":"apple","definitions":[{"string1":"this is string 1",
                                 "string2":"this is string 2""}]}]
r7knjye2

r7knjye21#

您的json为:

[
   {
      "word":"apple",
      "definitions":[
         {
            "string1":"this is string 1",
            "string2":"this is string 2"
         }
      ]
   }
]

从这个问题来看,您似乎对json不太熟悉,了解更多。
但现在你有一个 JSON Array ,包含 JSON Object . 又是什么 JSON Array 那已经 JSON Object .
因此,要回答您的问题,您应该访问以下内容:
请注意,此代码可能有一些错误,因为它未经测试。您有责任对其进行相应的修改。
步骤1:
得到 JSON ObjectJSON Array . 您提供了一些代码,所以我将做一些假设。

Item item = new Item(jsonObject.getString("word"), 
                     jsonObject.getString("string1"), jsonObject.getString("string2"));
``` `jsonObject` 已具有外部数组中的对象。如果是这种情况,请转到步骤2。否则,请继续阅读。

String json = "your json here"; // paste your provided json here
JSONArray jsonArray = new JSONArray(json);

// get the object at index 0
JSONObject jsonObject = jsonArray.getJSONObject(0);

// get the "word" value here
String word = jsonObject.getString("word");

步骤2:
现在我们已经提取了 `value` 关键 `word` . 让我们提取 `definitions` . 继续上一个示例。

// getting the json array for definition
JSONArray definitions = jsonObject.getJSONArray("definitions");

// get the object at index 1
// use loop if you want to get multiple values from this array
JSONObject first = definitions.getJSONObject(0);

// finally get your string values
String string1 = first.getString("string1");
String string2 = first.getString("string2");

完整代码:

String json = "your json here"; // paste your provided json here
JSONArray jsonArray = new JSONArray(json);

// get the object at index 0
JSONObject jsonObject = jsonArray.getJSONObject(0);

// get the "word" value here
String word = jsonObject.getString("word");

// getting the json array for definition
JSONArray definitions = jsonObject.getJSONArray("definitions");

// get the object at index 1
// use loop if you want to get multiple values from this array
JSONObject first = definitions.getJSONObject(0);

// finally get your string values
String string1 = first.getString("string1");
String string2 = first.getString("string2");

这就是你必须理解的 `JSON Array` 及 `JSON Object` 在你了解原因和方式之前。
w8f9ii69

w8f9ii692#

如果数据是对象,您可以尝试

var a = [{"word":"apple","definitions":[{"string1":"this is string 1","string2":"this is string 2"}]}]

var string1 = a[0]['definitions'][0]["string1"];
console.log('string 1 =>', string1);
var string2 = a[0]['definitions'][0]["string2"];
console.log('string 1 =>', string1);
rekjcdws

rekjcdws3#

如果您知道如何查看服务器响应,那么可以使用jsonschema2pojo生成模型就很酷了。所以你永远不会出错。
只要把你的答案抄在那里,你就会发现一切都是那么简单。从那时起,当我尝试此工具时,我不再手动执行此类转换。
如何从服务器获得响应?许多人使用改型库来实现这一点,在那里您将指定一个转换器(例如gson),它将答案转换为使用jsonschema2pojo创建的对象。
祝你好运

相关问题