首页 > 编程知识 正文

java串替换成json,字符串替换 java

时间:2024-04-24 11:43:03 阅读:333623 作者:XVZZ

本文目录一览:

java怎么把字符串转成json对象

下面以ali的fastjson为例,讲解json字符串转json对象:String jsonStr = "{"message" : "success"}";JSONObject jsonObject = JSON.parseObject(jsonStr);String message = jsonObject.getString("message");System.out.println(message);

java中字符串怎么转json

java字符串转json的话,你可以试试阿里巴巴提供的fastjson,fastjson号称是全球最好用的解析工具,使用非常简单,你只要Json.parseObject,或者Json.parseArray等等,就可以对字符串或者json转字符串互相转换。

java中字符串怎么转json?

string类型如果要转换成json的话,就需要写成这样的形式,如下:x0dx0aString jsonStr ="{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}";x0dx0a JSONObject jsonObj = new JSONObject(jsonStr);x0dx0a JSONObject refObj = new JSONObject(jsonObj.getString("refObj"));x0dx0a String existType = refObj.getString("existType");x0dx0a System.out.println(existType);x0dx0ajar使用的是org.json.jar

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怎么把字符串转换成jsonobject

转换方法:

import java.io.*;

import org.json.*;

public class Demo {

public static void main(String[] args) throws Exception {

String str = "{"id_no":"342422198012001122","mobile_no":"13666667789","name":"徐丽"}";

JSONObject obj = new JSONObject(str); //在这里转换。

System.out.println(obj);

System.out.println(obj.get("id_no")); // "342422198012001122"

}

}

要引入fast-json的包或者开源的包json.jar

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。