这是我的Volley抓取API请求的代码,我该如何解析?我想要像这样的东西:$response
val sq = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
//print the response
Log.i("GoogleIO","Response is : $response")
}, Response.ErrorListener {
//Log the error
Log.i("GoogleIO","That din't work")
})
//Add the request to the RequestQueue
Volley.newRequestQueue(this).add(sq)发布于 2019-12-15 05:26:27
假设您有以下json字符串作为响应
{
name: "John",
age: 31,
city: "New York"
}您可以像这样解析这个字符串
try {
JSONObject obj=new JSONObject(response);
String name=obj.getString("name");
int age=obj.getInt("age");
String city=obj.getString("city");
} catch (JSONException e) {
e.printStackTrace();
}发布于 2019-12-15 05:32:40
为此,您可以使用Gson:
首先,将依赖项放入应用程序级别的build.gradle文件中。
implementation 'com.google.code.gson:gson:2.8.6'然后,您可以添加以下内容:
var gson = new Gson()
var st = gson.toJson(response)
Log.i("GoogleIO","Response is : $st")https://stackoverflow.com/questions/59337317
复制相似问题