‘壹’ java怎么把字符串转成json字符串
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST)
@ResponseBody
public void updateInvestorApplyAccountNo(HttpServletRequest request,
HttpServletResponse response,
@RequestBody String requestBody) {
int num = 0;
String result = "";
//下面是把拿到的json字符串转成 json对象
JSONObject jsStr = JSONObject.parseObject(requestBody); //将字符串{“id”:1}
//int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值
/**
* json对象转换成java对象
*/
InvestorApplyModel stud = (InvestorApplyModel) JSONObject.toJavaObject(jsStr,InvestorApplyModel.class);
}
‘贰’ 如何将两个java数组拼接成json字符串 如下
...随便找个 JSON转换的插件吧,你先
把数组合并,然后 使用JSON 转换工具类 很easy ,常用的插件 gson,fastjson等等 网络一下很多
‘叁’ 怎样从java后台获取json字符串并转换为json对象输出
使用json-lib.jar这个工具x0dx0apublic String getJson(Object obj){x0dx0a JSONObject json;x0dx0a json = JSONObject.fromObject(obj);x0dx0a return json.toString();x0dx0a}x0dx0a使用jquery来处理jsonx0dx0a//转换为json数据 datas可以用ajax从后台获取上面getJson中的数据x0dx0avar jsonDatas = eval("(" + datas + ")");x0dx0a //循环遍历数据x0dx0ajQuery.each(jsonDatas, function(item) {x0dx0a//循环x0dx0a});
‘肆’ java如何返回json格式
例如:
Student st1 = new Student(1, "dg", 18, new Date());
Student st2 = new Student(2, "dg", 18, new Date());
Student st3 = new Student(3, "dg", 18, new Date());
Student st4 = new Student(4, "dg", 18, new Date());
Student st5 = new Student(5, "dg", 18, new Date());
List li = new ArrayList();
JSONObject JO1 = new JSONObject(st1);
JSONObject JO2 = new JSONObject(st2);
JSONObject JO3 = new JSONObject(st3);
JSONObject JO4 = new JSONObject(st4);
JSONObject JO5 = new JSONObject(st5);
li.add(JO1);
li.add(JO2);
li.add(JO3);
li.add(JO4);
li.add(JO5);
JSONArray Ja = new JSONArray(li);
Map ma = new HashMap();
ma.put("Result", "OK");
ma.put("Records", Ja);
JSONObject js = new JSONObject(ma);
out.print(js);
返回结果:
{"Result":"OK","Records":[{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":1},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":2},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":3},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":4},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":5}]}
‘伍’ java怎样进行json字符串拼接
StringBuffer
利用StringBuffer来拼接和截取,如果是拼接字符串强烈建议使用StringBuffer。这样可以防止内存泄露,否则大量用Str=str1+str2,会造成内存溢出用法如图。
‘陆’ java 怎么将string转为json数据
string转json有三种方法:
第一种:string直接转json
String json = "{"2":"efg","1":"abc"}"; JSONObject json_test =
JSONObject.fromObject(json); 将string的双引号转义即可,适用于字符串较短的
第二种:将string转为list后转为json
Listlist = new ArrayList(); list.add("username");
list.add("age"); list.add("sex"); JSONArray array = new JSONArray();
array.add(list);
可以使用list的add函数将需要的字符串拼接即可,但是这个只能使用jsonarry
第三种:将string转为map后转为json
Mapmap = new HashMap();
map.put("1", "abc");
map.put("2", "efg");
JSONArray array_test = new JSONArray();
array_test.add(map);
JSONObject jsonObject = JSONObject.fromObject(map);
这里使用map就可以将字符串转化为JSONArray或者JSONObject都可以,但是这里的键不能使用int型