Java 从 JSON 数组中提取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20058240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting data from JSON array
提问by user2855405
I know its an array, but I am completely new to JSON and need help comprehending how this is structured, here is my attempt at extracting data:
我知道它是一个数组,但我对 JSON 完全陌生,需要帮助理解它的结构,这是我提取数据的尝试:
String JSonString = readURL("//my URL is here");
JSONArray s = JSONArray.fromObject(JSonString);
JSONObject Data =(JSONObject)(s.getJSONObject(0));
System.out.println(Data.get("name"));
My JSON data that I have goes like this :
我的 JSON 数据是这样的:
{
"sports": [
{
"name": "basketball",
"id": 40,
"uid": "s:40",
"leagues": [
{
"name": "National Basketball Assoc.",
"abbreviation": "nba",
"id": 46,
"uid": "s:40~l:46",
"groupId": 7,
"shortName": "NBA",
"athletes": []
}
]
}
],
"resultsOffset": 10,
"resultsLimit": 10,
"resultsCount": 1,
"timestamp": "2013-11-18T03:15:43Z",
"status": "success"
}
I dont really have a strong grasp of this stuff so all the help is appreciated.
我对这些东西并不是很了解,所以感谢所有的帮助。
采纳答案by kiruwka
Here is the idea :
这是想法:
JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sport");
// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);
// and so on
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");
// and so on, you can process leaguesArrays similarily
It should work (feel free to complain about compile errors if there are any)
它应该可以工作(如果有任何编译错误,请随时抱怨)
回答by BSS
JSON means JavaScript Object Notation.
JSON 表示 JavaScript 对象表示法。
Objects in javascripts are just containers and can be represented by key-value pairs. Please find below notations to understand about json.
javascripts 中的对象只是容器,可以用键值对来表示。请查找以下符号以了解有关 json 的信息。
Represent objects in json: E.g. Student
在json中表示对象:例如Student
{"name" : "Robin", "rollnumber" : "1"}
Represent array in json : E.g. Array of students
在 json 中表示数组:例如学生数组
[{"name" : "Robin", "rollnumber" : "1"}, {"name" : "Mark", "rollnumber" : "2"}]
You can understand more on JSON from diagrams on this link http://www.json.org/fatfree.html
您可以通过此链接http://www.json.org/fatfree.html 上的图表了解有关 JSON 的更多信息
There are various ways available to to convert JSON to javaobject and javaobject to JSON : One of them is http://wiki.fasterxml.com/HymansonInFiveMinutes
有多种方法可以将 JSON 转换为 javaobject 和 javaobject 到 JSON:其中之一是http://wiki.fasterxml.com/HymansonInFiveMinutes
回答by Codo
Your JSON data is an object (it starts with a curly brace). In the next inner layer, there is a single array (at key "sports"):
您的 JSON 数据是一个对象(以花括号开头)。在下一个内层中,有一个数组(在“sports”键处):
String jsonString = readURL("//my URL is here");
JSONObject result = JSONObject(jsonString);
JSONArray sports = result.getJSONArray("sports");
JSONObject sport = sport.getJSONObject(0);
System.out.println(sport.getString("name"));
I might have used another JSON library than you.
我可能使用过其他 JSON 库而不是你。
回答by user3251882
Adding detailed code here along with the imports .
在此处添加详细代码以及导入。
If this helps.
如果这有帮助。
import org.json.JSONException;
import org.json.JSONObject;
public class extractingJSON {
public static void main(String[] args) throws JSONException {
// TODO Auto-generated method stub
String jsonStr = "{\"name\":\"SK\",\"arr\":{\"a\":\"1\",\"b\":\"2\"},\"arrArray\":[{\"a\":\"1\",\"b\":\"2\"}]}";
JSONObject jsonObj = new JSONObject(jsonStr);
String name = jsonObj.getString("name");
System.out.println(name);
String first = jsonObj.getJSONObject("arr").getString("a");
System.out.println(first);
first = jsonObj.getJSONArray("arrArray").getJSONObject(0).getString("a");
System.out.println(first);
}
}