嗨,我正尝试将JSON响应打印为可读形式,然后将其设置为Textview。这是我尝试打印JSON响应的代码。
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d("json", response);
//Creating JsonObject from response String
JSONObject jsonObject = new JSONObject(response);
//extracting json array from response string
JSONArray jsonArray = jsonObject.getJSONArray("data");
JSONObject jsonRow = jsonArray.getJSONObject(0);
//get value from jsonRow
leaderboardView.setText(jsonArray.toString());
它确实以JSON格式打印JSON。这是我正在接收的JSON格式。
{"data":[{"username":"DolanF","score":"4220","rank":"1"},{"username":"reyay","score":"3760","rank":"2"},{"username":"MeghanG","score":"2570","rank":"3"},{"username":"PrimGosling","score":"1360","rank":"4"},{"username":"JakubRozanski","score":"1190","rank":"5"},{"username":"rodyquigley","score":"1120","rank":"6"},{"username":"Kaz835","score":"800","rank":"7"},{"username":"bailey","score":"570","rank":"8"},{"username":"Ellis","score":"430","rank":"9"},{"username":"Joel","score":"390","rank":"10"}]}
我的目标是使用户名,排名和分数打印以可读的格式彼此相邻。
答案 0 :(得分:1)
更改:
jsonArray.toString()
进入:
jsonArray.toString(4)
参数(例如4
-与上面类似)是每个嵌套级别缩进的空格数。
这里有一个Kotlin示例,如何将所有数据作为变量:
fun readJson() {
val response =
"{\"data\":[{\"username\":\"DolanF\",\"score\":\"4220\",\"rank\":\"1\"},{\"username\":\"reyay\",\"score\":\"3760\",\"rank\":\"2\"},{\"username\":\"MeghanG\",\"score\":\"2570\",\"rank\":\"3\"},{\"username\":\"PrimGosling\",\"score\":\"1360\",\"rank\":\"4\"},{\"username\":\"JakubRozanski\",\"score\":\"1190\",\"rank\":\"5\"},{\"username\":\"rodyquigley\",\"score\":\"1120\",\"rank\":\"6\"},{\"username\":\"Kaz835\",\"score\":\"800\",\"rank\":\"7\"},{\"username\":\"bailey\",\"score\":\"570\",\"rank\":\"8\"},{\"username\":\"Ellis\",\"score\":\"430\",\"rank\":\"9\"},{\"username\":\"Joel\",\"score\":\"390\",\"rank\":\"10\"}]} \n" +
"\n"
val jsonObject = JSONObject(response)
val jsonArray = jsonObject.getJSONArray("data")
var output = ""
for (position in 0 until jsonArray.length()) {
val row = jsonArray.getJSONObject(position)
val name = row.getString("username")
val score = row.getString("score")
val rank = row.getInt("rank")
output += String.format("%s - %s (rank: %s)\n", name, score, rank)
}
text_view.text = output
}
或使用Java:
void readJson() {
String response =
"{\"data\":[{\"username\":\"DolanF\",\"score\":\"4220\",\"rank\":\"1\"},{\"username\":\"reyay\",\"score\":\"3760\",\"rank\":\"2\"},{\"username\":\"MeghanG\",\"score\":\"2570\",\"rank\":\"3\"},{\"username\":\"PrimGosling\",\"score\":\"1360\",\"rank\":\"4\"},{\"username\":\"JakubRozanski\",\"score\":\"1190\",\"rank\":\"5\"},{\"username\":\"rodyquigley\",\"score\":\"1120\",\"rank\":\"6\"},{\"username\":\"Kaz835\",\"score\":\"800\",\"rank\":\"7\"},{\"username\":\"bailey\",\"score\":\"570\",\"rank\":\"8\"},{\"username\":\"Ellis\",\"score\":\"430\",\"rank\":\"9\"},{\"username\":\"Joel\",\"score\":\"390\",\"rank\":\"10\"}]} \n" +
"\n";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
StringBuilder output = new StringBuilder();
for (int position = 0; position < jsonArray.length(); position++) {
JSONObject row = jsonArray.getJSONObject(position);
String name = row.getString("username");
String score = row.getString("score");
int rank = row.getInt("rank");
output.append(String.format("%s - %s (rank: %s)\n", name, score, rank));
}
text_views.setText(output.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}