如何通过改造解析 JSON 响应
How to parse a JSON response with retrofit
因此,我在 POST 电话
中给出了一个 API returns
{
"JHK":"One piece",
"LKJ":"Two pieces",
"OEN":"Three pieces"
}
因为这是一个列表,但从后端格式化的不好,我不知道如何从 Android 获取它,这就是我试过的方法
网络服务
@POST("get_user_codes.php")
suspend fun getUserCodesByMemberId(@Body member_id:String): CodesResponse
代码响应
data class CodesResponse(val data: List<Code>)
data class Code(val code_id:String,val name:String)
存储库
suspend fun getUserCodes(memberId:String):Result<CodesResponse>{
return Result.Success(webService.getUserCodesByMemberId(memberId))
}
但是他的响应超过了 Null,我真的不知道如何将那些应该是对象数组但实际上它们都在一个对象中的不同对象
有什么想法吗?
API 输入
member_id as text string
Example of input:
{ "member_id": "1"}
API 输出
code_id:作为字符串
名称:作为字符串
{
"JHK":"One piece",
"LKJ":"Two pieces",
"OEN":"Three pieces"
}
编辑
值可以超过我发的那3个,这取决于回复的多少returns
它将return null 因为JSON 里面有不同的键(“JHK”、“LKJ”等)。
由于 Retrofit 使用 GSON,您需要创建一个与 JSON 键同名的变量。您将必须使用 JSONObject 并解析响应。
而不是使用
@POST("get_user_codes.php")
暂停乐趣 getUserCodesByMemberId(@Body member_id:String): CodesResponse
使用
@POST("get_user_codes.php")
暂停乐趣 getUserCodesByMemberId(@Body member_id:String): JSONObject
假设你有这样的反应
String json = {
"JHK":"One piece",
"LKJ":"Two pieces",
"OEN":"Three pieces"
}
然后您可以获得忽略键的值列表,例如:
ArrayList<String> arr = new ArrayList<>();
try {
JSONObject response = new JSONObject(json);
Iterator keys = response.keys();
while (keys.hasNext()) {
// loop to get the dynamic key
String currentKey = (String) keys.next();
// get the value of the dynamic key
String value = response.getJSONObject(currentKey).toString();
arr.add(value);
}
} catch (Throwable t) {
Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
}
字段名称 JHK
LKJ
OEN
总是一样吗?你说可以超过3个,超过3个叫什么名字?
AbdelraZek 发布了一个很好的解决这个问题的方法:
我在 Kotlin 中的 Retrofit 版本:
改装:
// Here we use ScalarsConverter to be able to return String as a response.
Retrofit.Builder()
.baseUrl("http://YourURL")
.addConverterFactory(ScalarsConverterFactory.create())
.build()
.create(YourInterfaceClass::class.java)
// Then you can proceed to your POST function
suspend fun getUserCodesByMemberId(@Body member_id:String): String
// I suggest using Response<String> so you can check the response code and process it if needed.
然后,无论您需要什么,只需执行以下操作:
val response = getUserCodesByMemberId
val json = JSONObject(response.body()!!)
val array = mutableListOf<String>()
val keys: Iterator<String> = json.keys()
while (keys.hasNext()) {
val key = keys.next()
array.add(json[key].toString())
}
这样您就可以使用您不知道的 Json 响应。
因此,我在 POST 电话
中给出了一个 API returns{
"JHK":"One piece",
"LKJ":"Two pieces",
"OEN":"Three pieces"
}
因为这是一个列表,但从后端格式化的不好,我不知道如何从 Android 获取它,这就是我试过的方法
网络服务
@POST("get_user_codes.php")
suspend fun getUserCodesByMemberId(@Body member_id:String): CodesResponse
代码响应
data class CodesResponse(val data: List<Code>)
data class Code(val code_id:String,val name:String)
存储库
suspend fun getUserCodes(memberId:String):Result<CodesResponse>{
return Result.Success(webService.getUserCodesByMemberId(memberId))
}
但是他的响应超过了 Null,我真的不知道如何将那些应该是对象数组但实际上它们都在一个对象中的不同对象
有什么想法吗?
API 输入
member_id as text string
Example of input:
{ "member_id": "1"}
API 输出
code_id:作为字符串
名称:作为字符串
{
"JHK":"One piece",
"LKJ":"Two pieces",
"OEN":"Three pieces"
}
编辑
值可以超过我发的那3个,这取决于回复的多少returns
它将return null 因为JSON 里面有不同的键(“JHK”、“LKJ”等)。 由于 Retrofit 使用 GSON,您需要创建一个与 JSON 键同名的变量。您将必须使用 JSONObject 并解析响应。
而不是使用 @POST("get_user_codes.php") 暂停乐趣 getUserCodesByMemberId(@Body member_id:String): CodesResponse
使用 @POST("get_user_codes.php") 暂停乐趣 getUserCodesByMemberId(@Body member_id:String): JSONObject
假设你有这样的反应
String json = {
"JHK":"One piece",
"LKJ":"Two pieces",
"OEN":"Three pieces"
}
然后您可以获得忽略键的值列表,例如:
ArrayList<String> arr = new ArrayList<>();
try {
JSONObject response = new JSONObject(json);
Iterator keys = response.keys();
while (keys.hasNext()) {
// loop to get the dynamic key
String currentKey = (String) keys.next();
// get the value of the dynamic key
String value = response.getJSONObject(currentKey).toString();
arr.add(value);
}
} catch (Throwable t) {
Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
}
字段名称 JHK
LKJ
OEN
总是一样吗?你说可以超过3个,超过3个叫什么名字?
AbdelraZek 发布了一个很好的解决这个问题的方法:
我在 Kotlin 中的 Retrofit 版本:
改装:
// Here we use ScalarsConverter to be able to return String as a response.
Retrofit.Builder()
.baseUrl("http://YourURL")
.addConverterFactory(ScalarsConverterFactory.create())
.build()
.create(YourInterfaceClass::class.java)
// Then you can proceed to your POST function
suspend fun getUserCodesByMemberId(@Body member_id:String): String
// I suggest using Response<String> so you can check the response code and process it if needed.
然后,无论您需要什么,只需执行以下操作:
val response = getUserCodesByMemberId
val json = JSONObject(response.body()!!)
val array = mutableListOf<String>()
val keys: Iterator<String> = json.keys()
while (keys.hasNext()) {
val key = keys.next()
array.add(json[key].toString())
}
这样您就可以使用您不知道的 Json 响应。