我正在使用json api开发一个应用程序,并希望在我的应用程序中实现以下格式api。我如何使用android studio 2.3在我的项目中实现这个?
{
"getdetailsasanas": [
{
"asanaid": 1,
"asananame": "Half easy gas release pose",
"duration": 1,
"imageurl": "http://www.yogapoint.com/iOS/images/half-easy-gas-release-pose.jpg",
"imageversion": 2,
"audiourl": "http://www.yogapoint.com/iOS/audio/half-easy-gas-release-pose.mp3",
"audioversion": 1,
"videourl": "NULL",
"videoversion": 0,
"StepsForAsana": [
{
"stepnumber": 1,
"stepdesc": "Step 1",
"stepimg": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step1.jpg",
"stepimgeversion": 0,
"imgethumbversion": 1,
"imgethumburl": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step1-tb.jpg"
},
{
"stepnumber": 2,
"stepdesc": "Step 2",
"stepimg": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step2.jpg",
"stepimgeversion": 0,
"imgethumbversion": 1,
"imgethumburl": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step2-tb.jpg"
}
]
}
]
}
答案 0 :(得分:0)
您可以使用其默认情况下随Android SDK提供的JSONObject:
String json = "some JSON";
JSONObject jsonObject = new JSONObject(json);
比你可以使用像
这样的方法getBoolean(String key)
getDouble(String key)
等
或者您可以使用google gson库,这在我看来更好,但您需要将其作为依赖项包含在gradle中。
答案 1 :(得分:0)
使用Gson库。解析响应的最佳方法。您可以使用GsonFormat插件生成响应模型。
答案 2 :(得分:0)
查看gson。这是json的一个简单的序列化器。
答案 3 :(得分:0)
很高兴回答你的问题。 你可以这样做。
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("getdetailsasanas");
for (int i = 0; i < jsonArray.length(); i++) {
String asanaid = jsonArray.getJSONObject(i).getString("asanaid");
String asananame = jsonArray.getJSONObject(i).getString("asananame");
String duration = jsonArray.getJSONObject(i).getString("duration");
String imageurl = jsonArray.getJSONObject(i).getString("imageurl");
String imageversion = jsonArray.getJSONObject(i).getString("imageversion");
String videourl = jsonArray.getJSONObject(i).getString("videourl");
String videoversion = jsonArray.getJSONObject(i).getString("videoversion");
JSONArray StepsForAsanaArray = jsonArray.getJSONObject(i).getJSONArray("StepsForAsana");
for (int j = 0; j < StepsForAsanaArray.length(); j++) {
String stepnumber = jsonArray.getJSONObject(j).getString("stepnumber");
String stepdesc = jsonArray.getJSONObject(j).getString("stepdesc");
String stepimg = jsonArray.getJSONObject(j).getString("stepimg");
String stepimgeversion = jsonArray.getJSONObject(j).getString("stepimgeversion");
String imgethumbversion = jsonArray.getJSONObject(j).getString("imgethumbversion");
String imgethumburl = jsonArray.getJSONObject(j).getString("imgethumburl");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 4 :(得分:0)
与此代码一样使用:
JSONArray getdetailsasanas = new JSONArray(stringData);
for (int p = 0; p < getdetailsasanas.length(); p++) {
try {
JSONObject data = getdetailsasanas.getJSONObject(p);
JSONObject asanaid = data.getJSONObject("asanaid");
JSONObject stepnumber = data.getJSONObject("stepnumber");
JSONObject stepnumber = data.getJSONObject("stepnumber");
//get data from objetcs
String asananame = asanaid.getString("asananame");
int duration = asanaid.getInt("duration");
String imageurl = asanaid.getString("imageurl");
} catch (JSONException e) {
e.printStackTrace();
}
}