Java 将JsonObject存储到HasMap中
我的反应越来越差 在每个响应中,键和值都会发生变化。 我想在HashMap中存储count和previousCountDay、键和值 Json响应:Java 将JsonObject存储到HasMap中,java,android,android-json,gson,Java,Android,Android Json,Gson,我的反应越来越差 在每个响应中,键和值都会发生变化。 我想在HashMap中存储count和previousCountDay、键和值 Json响应: { "count": { "2018-03-28 18": 55, "2018-03-28 19": 48, "2018-03-28 20": 41, "2018-03-28 21": 31, "2018-03-28 22": 32, "2018
{
"count": {
"2018-03-28 18": 55,
"2018-03-28 19": 48,
"2018-03-28 20": 41,
"2018-03-28 21": 31,
"2018-03-28 22": 32,
"2018-03-28 23": 26,
"2018-03-29 00": 20,
"2018-03-29 01": 16,
"2018-03-29 02": 12,
"2018-03-29 03": 0
},
"previousCountDay": {
"2018-03-27 18": 40,
"2018-03-27 19": 59,
"2018-03-27 20": 53,
"2018-03-27 21": 48,
"2018-03-27 22": 36,
"2018-03-27 23": 40,
"2018-03-28 00": 37,
"2018-03-28 01": 14,
"2018-03-28 02": 29,
"2018-03-28 03": 1
},
"noOfIntervals": 10,
"range": [
"18",
"19",
"20",
"21",
"22",
"23",
"00",
"01",
"02",
"03"
]
}
private void JsonRequestOrderVelocity(String dmhw) {
utils.showDialog();
String url = Constants.VELOCITY_API;
Log.e("URL", "" + url);
JsonObjectRequest request = new JsonObjectRequest(url, null,
response -> {
Log.e("onResponse",""+response);
try {
Gson gson = new Gson();
Type listType = new TypeToken<OrderVelocityPojo>() {
}.getType();
orderVelocityPojo = gson.fromJson(response.toString(), listType);
Log.e("SIZE",""+orderVelocityPojo.getRange().size());
JSONObject object = response.getJSONObject("count");
Map<String, Integer> countMap = new HashMap<String, Integer>();
//store keys and values in HashMap.
for(int i=0;i<orderVelocityPojo.getRange().size();i++){
countMap.put( );
}
} catch (Exception e) {
Log.e("Exception",""+e);
utils.hideDialog();
e.printStackTrace();
}
utils.hideDialog();
}, error -> {
Log.e("error",""+error.getMessage());
utils.hideDialog();
});
request.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance(this).addToRequestQueue(request);
}
HashMap<String, Integer> count = new HashMap<>();
try {
JSONObject object = response.getJSONObject("count");
Iterator a = object.keys();
while (a.hasNext()) {
String key = (String) a.next();
// loop to get the dynamic key
Integer value = (Integer) object.get(key);
Log.e("count : ","Keys :"+ key+" Values :"+value);
count.put(key, value);
}
} catch (JSONException e) {
e.printStackTrace();
}
通过使用GSON,我得到了JSON响应,但我只存储了range
,因为它是在JSON数组中,通过range
我得到了count
和previousCountDay
的大小
下面是我的活动课:
{
"count": {
"2018-03-28 18": 55,
"2018-03-28 19": 48,
"2018-03-28 20": 41,
"2018-03-28 21": 31,
"2018-03-28 22": 32,
"2018-03-28 23": 26,
"2018-03-29 00": 20,
"2018-03-29 01": 16,
"2018-03-29 02": 12,
"2018-03-29 03": 0
},
"previousCountDay": {
"2018-03-27 18": 40,
"2018-03-27 19": 59,
"2018-03-27 20": 53,
"2018-03-27 21": 48,
"2018-03-27 22": 36,
"2018-03-27 23": 40,
"2018-03-28 00": 37,
"2018-03-28 01": 14,
"2018-03-28 02": 29,
"2018-03-28 03": 1
},
"noOfIntervals": 10,
"range": [
"18",
"19",
"20",
"21",
"22",
"23",
"00",
"01",
"02",
"03"
]
}
private void JsonRequestOrderVelocity(String dmhw) {
utils.showDialog();
String url = Constants.VELOCITY_API;
Log.e("URL", "" + url);
JsonObjectRequest request = new JsonObjectRequest(url, null,
response -> {
Log.e("onResponse",""+response);
try {
Gson gson = new Gson();
Type listType = new TypeToken<OrderVelocityPojo>() {
}.getType();
orderVelocityPojo = gson.fromJson(response.toString(), listType);
Log.e("SIZE",""+orderVelocityPojo.getRange().size());
JSONObject object = response.getJSONObject("count");
Map<String, Integer> countMap = new HashMap<String, Integer>();
//store keys and values in HashMap.
for(int i=0;i<orderVelocityPojo.getRange().size();i++){
countMap.put( );
}
} catch (Exception e) {
Log.e("Exception",""+e);
utils.hideDialog();
e.printStackTrace();
}
utils.hideDialog();
}, error -> {
Log.e("error",""+error.getMessage());
utils.hideDialog();
});
request.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance(this).addToRequestQueue(request);
}
HashMap<String, Integer> count = new HashMap<>();
try {
JSONObject object = response.getJSONObject("count");
Iterator a = object.keys();
while (a.hasNext()) {
String key = (String) a.next();
// loop to get the dynamic key
Integer value = (Integer) object.get(key);
Log.e("count : ","Keys :"+ key+" Values :"+value);
count.put(key, value);
}
} catch (JSONException e) {
e.printStackTrace();
}
private void JsonRequestOrderVelocity(字符串dmhw){
utils.showDialog();
字符串url=Constants.VELOCITY\u API;
Log.e(“URL”,“URL+URL”);
JsonObjectRequest=新的JsonObjectRequest(url,null,
答复->{
Log.e(“onResponse”,即“+response”);
试一试{
Gson Gson=新的Gson();
类型listType=新类型令牌(){
}.getType();
orderVelocityPojo=gson.fromJson(response.toString(),listType);
Log.e(“大小”,“orderVelocityPojo.getRange().SIZE());
JSONObject object=response.getJSONObject(“count”);
Map countMap=新的HashMap();
//在HashMap中存储键和值。
对于(int i=0;i{
Log.e(“error”,“”+error.getMessage());
utils.hideDialog();
});
request.setRetryPolicy(新的DefaultRetryPolicy(
我的插座超时,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance(this).addToRequestQueue(请求);
}
OrderVelocityPojoPojo类:
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class OrderVelocityPojo {
@SerializedName("noOfIntervals")
@Expose
private Integer noOfIntervals;
@SerializedName("range")
@Expose
private List<String> range = null;
public Integer getNoOfIntervals() {
return noOfIntervals;
}
public void setNoOfIntervals(Integer noOfIntervals) {
this.noOfIntervals = noOfIntervals;
}
public List<String> getRange() {
return range;
}
public void setRange(List<String> range) {
this.range = range;
}
}
import java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类OrderVelocityPojo{
@序列化名称(“noOfIntervals”)
@暴露
私有整数noOfIntervals;
@序列化名称(“范围”)
@暴露
私有列表范围=空;
公共整数getNoOfIntervals(){
返回noOfIntervals;
}
公共void setNoOfIntervals(整数noOfIntervals){
this.noOfIntervals=noOfIntervals;
}
公共列表getRange(){
返回范围;
}
公共无效设置范围(列表范围){
这个范围=范围;
}
}
您可以为此使用库。
您可以尝试将json对象转换为hashmap。这里您将提供typeToken作为json类型值
Map<String, Object> hashmap = new Gson().fromJson(
jsonString, new TypeToken<HashMap<String, Object>>() {}.getType()
);
Map hashmap=new Gson().fromJson(
jsonString,新的TypeToken(){}.getType()
);
您还可以使用JSONObject
的迭代键手动添加数据
下面是示例代码
HashMap<String, String> count = new HashMap<>();
HashMap<String, String> previousCountDay = new HashMap<>();
try {
JSONObject mJsonObjectMain = new JSONObject("your json string");
JSONObject mJsonObjectCount = mJsonObjectMain.getJSONObject("count");
Iterator a = mJsonObjectCount.keys();
while (a.hasNext()) {
String key = (String) a.next();
// loop to get the dynamic key
String value = (String) mJsonObjectCount.get(key);
System.out.print("key : " + key);
System.out.println(" value :" + value);
count.put(key, value);
}
JSONObject mJsonObjectPreviousCount = mJsonObjectMain.getJSONObject("previousCountDay");
//do same as above
} catch (JSONException e) {
e.printStackTrace();
}
HashMap count=newhashmap();
HashMap previousCountDay=新HashMap();
试一试{
JSONObject mJsonObjectMain=新的JSONObject(“您的json字符串”);
JSONObject mJsonObjectCount=mJsonObjectMain.getJSONObject(“count”);
迭代器a=mJsonObjectCount.keys();
while(a.hasNext()){
字符串键=(字符串)a.next();
//循环以获取动态密钥
字符串值=(字符串)mJsonObjectCount.get(键);
系统输出打印(“键:+键);
System.out.println(“值:”+value);
count.put(键、值);
}
JSONObject MJSONObject PreviousCount=MJSONObject main.getJSONObject(“previousCountDay”);
//同上
}捕获(JSONException e){
e、 printStackTrace();
}
最终代码:
{
"count": {
"2018-03-28 18": 55,
"2018-03-28 19": 48,
"2018-03-28 20": 41,
"2018-03-28 21": 31,
"2018-03-28 22": 32,
"2018-03-28 23": 26,
"2018-03-29 00": 20,
"2018-03-29 01": 16,
"2018-03-29 02": 12,
"2018-03-29 03": 0
},
"previousCountDay": {
"2018-03-27 18": 40,
"2018-03-27 19": 59,
"2018-03-27 20": 53,
"2018-03-27 21": 48,
"2018-03-27 22": 36,
"2018-03-27 23": 40,
"2018-03-28 00": 37,
"2018-03-28 01": 14,
"2018-03-28 02": 29,
"2018-03-28 03": 1
},
"noOfIntervals": 10,
"range": [
"18",
"19",
"20",
"21",
"22",
"23",
"00",
"01",
"02",
"03"
]
}
private void JsonRequestOrderVelocity(String dmhw) {
utils.showDialog();
String url = Constants.VELOCITY_API;
Log.e("URL", "" + url);
JsonObjectRequest request = new JsonObjectRequest(url, null,
response -> {
Log.e("onResponse",""+response);
try {
Gson gson = new Gson();
Type listType = new TypeToken<OrderVelocityPojo>() {
}.getType();
orderVelocityPojo = gson.fromJson(response.toString(), listType);
Log.e("SIZE",""+orderVelocityPojo.getRange().size());
JSONObject object = response.getJSONObject("count");
Map<String, Integer> countMap = new HashMap<String, Integer>();
//store keys and values in HashMap.
for(int i=0;i<orderVelocityPojo.getRange().size();i++){
countMap.put( );
}
} catch (Exception e) {
Log.e("Exception",""+e);
utils.hideDialog();
e.printStackTrace();
}
utils.hideDialog();
}, error -> {
Log.e("error",""+error.getMessage());
utils.hideDialog();
});
request.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance(this).addToRequestQueue(request);
}
HashMap<String, Integer> count = new HashMap<>();
try {
JSONObject object = response.getJSONObject("count");
Iterator a = object.keys();
while (a.hasNext()) {
String key = (String) a.next();
// loop to get the dynamic key
Integer value = (Integer) object.get(key);
Log.e("count : ","Keys :"+ key+" Values :"+value);
count.put(key, value);
}
} catch (JSONException e) {
e.printStackTrace();
}
HashMap count=newhashmap();
试一试{
JSONObject object=response.getJSONObject(“count”);
迭代器a=object.keys();
while(a.hasNext()){
字符串键=(字符串)a.next();
//循环以获取动态密钥
整数值=(整数)对象。获取(键);
Log.e(“计数:”,“键:“+key+”值:“+value”);
count.put(键、值);
}
}捕获(JSONException e){
e、 printStackTrace();
}
你能展示你的OrderVelocityPojo类吗?由于键在变化,很难定义一个Pojo来反序列化json。你可以将json反序列化为对象或映射,在这两种情况下,GSON都会从你的响应中形成一个嵌套映射。你可以从映射中获取count-Map。尽管你必须执行一些类型转换。我添加了我的Pojo class@Debanjani如果“count”键不变,您可以在pojo中添加Map count;,否则您可能不得不放弃pojo并改用Map。