NutzCN Logo
问答 list的json字符串如何接收并如何处理
发布于 383天前 作者 qq_4f7a40b4 259 次浏览 复制 上一个帖子 下一个帖子
标签:

移动端以[{'key1':'value1'},{'key2':'value2'},{'key3':'value3'}]传过来的字符串参数,以@Param("::list") List xxx接收易于处理还是@Param("list") String xxx易于处理

8 回复

外面不包一个map?? 那整个数据就是List了, 前缀表单是取不到的.

public void xxxxx(List<Map<String, Object> list) 

有map,整个参数为{"list":"[{},{},{}]","key2":"xxx","key3":"dfhdsklh"}

那就是

public void xxxxx(@Param("list")List<Map<String, Object> list) 

这样的话将map转换成实体类就比较麻烦了,不能自动做实体类的匹配,发出请求的源码如下:
HashMap<String, String> resultHash = new HashMap<String, String>();
List<HashMap<String,String>> arr = new ArrayList<HashMap<String,String>> ;
for (int i = 0; i < list.size(); i++) {
hash.put("extendInfo", StringUtil.trim(workDiary.getExtendInfo()));
hash.put("safeInfo", StringUtil.trim(workDiary.getSafeInfo()));
hash.put("weather", StringUtil.trim(workDiary.getWeather()));
hash.put("modifytime", StringUtil.trim(workDiary.getLocalModifyTime()));
arr.put(hash);
}
resultHash.put("list", arr);
PCHttpUtils.httpPost(url, resultHash, Constants.CHARSET, true);

参数接收如下:
@At("/insertOrReplaceList")
public Object insertOrReplaceList(@Param("::list") List list) {
// 判断登录状态
Results result = loginService.chargeAppType();
if (!result.getCode().equals(Results.SUCCESS)) {
return result;
}
String appType = Mvcs.getReq().getHeader("app_type");
StringBuffer idsSb = new StringBuffer("-1");
if (list != null && list.size() > 0) {
for (WorkDiary obj : list) {
obj.setExtend1(appType);
idsSb.append("," + obj.getId());
if (Utils.isNullOrEmpty(obj.getId())) {
return Results.parse(Results.FAIL, "您传递的信息在您本地不存在");
}
}
. ......

看清楚@Param里面的内容

public void xxxxx(@Param("list")List<User> list) 

要疯了,加了适配器以后一直报错,进不了方法了java.lang.NumberFormatException
at java.math.BigDecimal.(Unknown Source)
at java.math.BigDecimal.(Unknown Source)
at org.nutz.json.impl.JsonTokenScan.readObject(JsonCompileImplV2.java:282)
at org.nutz.json.impl.JsonTokenScan.read(JsonCompileImplV2.java:351)
at org.nutz.json.impl.JsonCompileImplV2.parse(JsonCompileImplV2.java:25)
at org.nutz.json.Json.fromJson(Json.java:40)
at org.nutz.mvc.adaptor.JsonAdaptor.getReferObject(JsonAdaptor.java:35)

请求:
public static String httpPostNew(String url, HashMap<String, Object> params, String charset, boolean encrypt) {
String result = "";
Request req = Request.create(url, METHOD.POST);
for (String key:params.keySet()) {
req.getParams().put(key, params.get(key));
}
req.setEnc(charset);
Header header=req.getHeader();
header.set("Content-Type","application/json");
req.setHeader(header);
Response resp = new FilePostSender(req).setTimeout(5 * 60 * 1000)
.send();
if (resp.isOK()) {
result = resp.getContent();
}
return result;
}

安卓端代码:
JSONArray arr = new JSONArray();
// List<HashMap<String,String>> arr = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < list.size(); i++) {
WorkRecord workrecord = list.get(i);
String type = StringUtil.trim(workrecord.getLocalModifyState()).toUpperCase();

              HashMap<String, String> hash = new HashMap<String, String>();
              hash.put("id", StringUtil.trim(workrecord.getId()));
              hash.put("proId", StringUtil.trim(workrecord.getProId()));
              hash.put("recordTitle", StringUtil.trim(workrecord.getRecordTitle()));
              hash.put("recordType", StringUtil.trim(workrecord.getRecordType()));
                                           arr.put(hash);
}
                                    resultHash.put("list", arr);

// resultHash.put("list", arr.toString());
AuthUtil.setAuthNew(resultHash, userInfo.getId(), SecretUtil.decrypt(userInfo.getKey()),
userInfo.getUserName());
String s = PCHttpUtils.httpPostNew(url, resultHash, Constants.CHARSET, true);
logFlag = s;

你确定发送的是json字符串??? 报的是json解析错误.

// 这个httpPostNew 会把resultHash转为JSON?? 看上去就不像
PCHttpUtils.httpPostNew(url, resultHash, Constants.CHARSET, true);

PS: "回复"按钮旁边就是 "插入代码"的按钮!!!

添加回复
请先登陆
回到顶部