关闭。这个问题需要详细或明确。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,添加细节并澄清问题。
五小时前关门了。
改进这个问题
我想得到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""}]}]
3条答案
按热度按时间r7knjye21#
您的json为:
从这个问题来看,您似乎对json不太熟悉,了解更多。
但现在你有一个
JSON Array
,包含JSON Object
. 又是什么JSON Array
那已经JSON Object
.因此,要回答您的问题,您应该访问以下内容:
请注意,此代码可能有一些错误,因为它未经测试。您有责任对其进行相应的修改。
步骤1:
得到
JSON Object
从JSON Array
. 您提供了一些代码,所以我将做一些假设。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");
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");
w8f9ii692#
如果数据是对象,您可以尝试
rekjcdws3#
如果您知道如何查看服务器响应,那么可以使用jsonschema2pojo生成模型就很酷了。所以你永远不会出错。
只要把你的答案抄在那里,你就会发现一切都是那么简单。从那时起,当我尝试此工具时,我不再手动执行此类转换。
如何从服务器获得响应?许多人使用改型库来实现这一点,在那里您将指定一个转换器(例如gson),它将答案转换为使用jsonschema2pojo创建的对象。
祝你好运