JSON数据格式的转化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jcxjava/article/details/79975626

一、json格式字符串转json对象。

    1、json对象字符串:String jsonStr = "{'code':1,'msg':'success'}";

        转化成json对象:JSONObject jsonObject = JSONObject.fromObject(jsonStr); //{"code":1,"msg":"success"}

        获取节点数据:String msg = jsonObject.getString("msg"); //success

    2、json数组字符串:String jsonArr = "[{'code':0,'msg':'fail'},{'code':1,'msg':'success'}]";

        转化成json数组:JSONSArray jsonArray = JSONArray.fromObject(jsonArr);

        遍历取值...

二、json对象转实体类

1.定义实体类:

public class Entity {
    private String code;
    private String msg;

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

}

2.用toBean()方法转化为实体类。

Entity jsonDemo = (Entity) JSONObject.toBean(jsonObject,Entity.class);

三、json和list互转

1.json转list

String jsonArr = "[{'code':0,'msg':'fail'},{'code':1,'msg':'success'}]";

JSONArray jsonArray = JSONArray.fromObject(jsonArr);

List<Entity> list = JSONArray.toList(jsonArray,Entity.class);

2.list转json

JSONArray listArray=JSONArray.fromObject(lists);



JSON转化

03-03

[color=#FF0000]Action部分的代码[/color]rnrnimport java.io.IOException;rnimport java.util.HashMap;rnimport java.util.Map;rnrnimport org.apache.struts2.ServletActionContext;rnimport org.apache.struts2.convention.annotation.Action;rnimport org.apache.struts2.convention.annotation.Namespace;rnimport org.apache.struts2.convention.annotation.ParentPackage;rnimport org.springframework.beans.factory.annotation.Autowired;rnrnimport com.alibaba.fastjson.JSON;rnimport com.model.User;rnimport com.opensymphony.xwork2.ActionSupport;rnimport com.service.dao.UserServiceDao;rnrn.........rnpublic void login() rn User u = userServiceDao.login(user);rn System.out.println(u);rn Map m = new HashMap();rn if (u != null) rn m.put("success", true);rn m.put("message", "登陆成功");rn String json = JSON.toJSONString(m);rn try rn ServletActionContext.getResponse().setCharacterEncoding("UTF-8");rn ServletActionContext.getResponse().setContentType(rn "text/html;charset=utf-8");rn ServletActionContext.getResponse().getWriter().write(json);rn ServletActionContext.getResponse().getWriter().flush();rn ServletActionContext.getResponse().getWriter().close();rn catch (IOException e) rn e.printStackTrace();rn rn else rn m.put("message", "登陆失败");rn String json = JSON.toJSONString(m);rn try rn ServletActionContext.getResponse().setCharacterEncoding("UTF-8");rn ServletActionContext.getResponse().setContentType(rn "text/html;charset=utf-8");rn ServletActionContext.getResponse().getWriter().write(json);rn ServletActionContext.getResponse().getWriter().flush();rn ServletActionContext.getResponse().getWriter().close();rn catch (IOException e) rn e.printStackTrace();rn rn rn rnrnrn[color=#FF0000]script代码[/color]rnrn rnrnalert提示[object Object]而firefox在前台有响应"message":"登陆成功","success",truern利用jQuery.parseJSON(data)也不行,求高手!!!!!

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试