如何在哈希图中获取字符串和哈希图混合值
How to get string and hashmap mixed value inside a hashmap
我正在使用 volley 并尝试向我正在处理的 API 发出请求。 Json请求应该是这样的格式。
{
"name": "API name",
"param":{
"email": "user@mail.com",
"password": "password"
}
}
我尝试过使用 hashmap 但我不知道如何放置
<string string>
<string, hashmap>
这变得越来越复杂了。
现在,我应该如何将这些值放入 hashmap 并将其转换为 JSONObject 并向服务器发送请求。
如果这不是应该的方式,那么我应该改用什么?
HahMap不能用于JSON序列化,建议使用org.json(https://mvnrepository.com/artifact/org.json/json)
例如:
JSONObject jsonObj = new JSONObject();
JSONObject param = new JSONObject();
param.put("email","blhablah");
param.put("password","blhablah");
jsonObj.put("name", "apiName");
jsonObj.put("param", param);
System.out.println(jsonObj.toString());
这将为您提供 json,如下所示:
{"param":{"password":"blhablah","email":"blhablah"},"name":"apiName"}
我正在使用 volley 并尝试向我正在处理的 API 发出请求。 Json请求应该是这样的格式。
{
"name": "API name",
"param":{
"email": "user@mail.com",
"password": "password"
}
}
我尝试过使用 hashmap 但我不知道如何放置
<string string>
<string, hashmap>
这变得越来越复杂了。
现在,我应该如何将这些值放入 hashmap 并将其转换为 JSONObject 并向服务器发送请求。
如果这不是应该的方式,那么我应该改用什么?
HahMap不能用于JSON序列化,建议使用org.json(https://mvnrepository.com/artifact/org.json/json)
例如:
JSONObject jsonObj = new JSONObject();
JSONObject param = new JSONObject();
param.put("email","blhablah");
param.put("password","blhablah");
jsonObj.put("name", "apiName");
jsonObj.put("param", param);
System.out.println(jsonObj.toString());
这将为您提供 json,如下所示:
{"param":{"password":"blhablah","email":"blhablah"},"name":"apiName"}