
我低于JsonResponse.
在每个响应中,键和值都改变.
我要存储计数& HashMap中的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-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" ]}通过使用GSON,我得到了JsON响应,但是我只存储范围,因为它即将出现在JsON数组中.按范围,我得到的是count& prevIoUsCountDay.
以下是我的活动课程:
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);}OrderVeLocityPojo Pojo类:
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; }}解决方法:
您还可以使用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<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(); } 总结 以上是内存溢出为你收集整理的java-将JsonObject存储到HasMap中全部内容,希望文章能够帮你解决java-将JsonObject存储到HasMap中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)