【发布时间】:2018-04-13 14:29:19
【问题描述】:
我尝试了不同的方法来解析 Android 中的 JSON 文件,但在打印出来时遇到了错误。
这是我的整个 JSON 文件:
{
conferences: [
{
id: "7ab1c9c0-4ffa-4f27-bcb6-be54d6ca6127",
name: "EASTERN CONFERENCE",
alias: "EASTERN",
divisions: [
{
id: "1fad71d8-5b9e-4159-921b-9b98d0573f51",
name: "Atlantic",
alias: "ATLANTIC",
teams: [
{
id: "4417d3cb-0f24-11e2-8525-18a905767e44",
name: "Lightning",
market: "Tampa Bay",
reference: "14",
rank: {
division: 1,
conference: 1,
clinched: "conference"
}
},
{
id: "4416ba1a-0f24-11e2-8525-18a905767e44",
name: "Bruins",
market: "Boston",
reference: "6",
rank: {
division: 2,
conference: 2,
clinched: "playoff_spot"
}
},
]
},
{
id: "d5ec45c4-a691-43ac-9e6d-92de92e763e7",
name: "Metropolitan",
alias: "METROPOLITAN",
teams: [
{
id: "4417eede-0f24-11e2-8525-18a905767e44",
name: "Capitals",
market: "Washington",
reference: "15",
rank: {
division: 1,
conference: 3,
clinched: "division"
}
},
]
}
]
},
{
id: "64901512-9ca9-4bea-aa80-16dbcbdae230",
name: "WESTERN CONFERENCE",
alias: "WESTERN",
divisions: [
{
id: "5e868c4d-c6a3-4901-bc3c-3b7a4509e402",
name: "Central",
alias: "CENTRAL",
teams: [
{
id: "441643b7-0f24-11e2-8525-18a905767e44",
name: "Predators",
market: "Nashville",
reference: "18",
rank: {
division: 1,
conference: 1,
clinched: "presidents_trophy"
}
}
]
},
{
id: "17101b65-e8b9-4cda-a963-eea874aed81f",
name: "Pacific",
alias: "PACIFIC",
teams: [
{
id: "42376e1c-6da8-461e-9443-cfcf0a9fcc4d",
name: "Golden Knights",
market: "Vegas",
reference: "54",
rank: {
division: 1,
conference: 3,
clinched: "division"
}
},
]
}
]
}
]
}
这是我为使其工作而实现的代码:
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray con = jsonObj.getJSONArray("conferences");
JSONArray division = con.getJSONArray("divisions");
JSONArray teams = jsonObj.getJSONArray("teams");
for (int i = 0; i < teams.length(); i++) {
JSONObject c = con.getJSONObject(i);
String alias = c.getString("alias");
JSONObject t = teams.getJSONObject(i);
String name = t.getString("name");
JSONObject rank = t.getJSONObject("rank");
String div = rank.getString("division");
// tmp hash map for single fixture
HashMap<String, String> fixture = new HashMap<>();
// adding each child node to HashMap key => value
// fixture.put("alias", alias);
fixture.put("name", name);
fixture.put("division", div);
// adding contact to contact list
fixtureList.add(fixture);
}
这里是 onPostExecute 方法:
@Override
protected void onPostExecute(Void aVoid) {
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, fixtureList,
R.layout.list_ranking_item, new String[]{/**"alias"**/ "name", "div"},
new int[]{/**R.id.ranking_name**/ R.id.ranking_team,
R.id.ranking_rank});
lv.setAdapter(adapter);
}
我已从该站点获得帮助来布局我的功能:https://www.androidhive.info/2012/01/android-json-parsing-tutorial/
【问题讨论】:
-
使用jsonschema2pojo.org为你的json文件获取对应的java类模型。在 build.gradle 中添加 gson,然后添加 new Gson().fromJson(yourJsonString,YourModelClass.class,这将为您提供模型类的对象,该对象将具有解析的 json
-
@AshishKumar 有没有不用Gson直接做的方法?
-
当然,只是你必须正确解析它。粘贴整个 json 字符串,您的图像不会显示整个 json
-
@AshishKumar 我已经更新了我的 JSON 文件。我希望现在清楚了。
-
我已经添加了相同的解析器