java的json字元轉
1. 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);
}
2. java怎麼將json字元串轉換為json對象
轉換方法:
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
3. java中字元串怎麼轉json
string類型如果要轉換成json的話,就需要寫成這樣的形式,如下:
String jsonStr ="{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}";
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject refObj = new JSONObject(jsonObj.getString("refObj"));
String existType = refObj.getString("existType");
System.out.println(existType);
jar使用的是org.json.jar
4. java中,json格式的字元串轉換成對象
要使來程序可以運源行必須引入JSON-lib包,JSON-lib包同時依賴於以下的JAR包:
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar
5. java中怎麼json字元串轉為json對象
需要引入fastjsonjar包
publicclassFastjson{
publicstaticvoidmain(String[]args){
Stringss="{'results':[{'location':{'id':'WTG7R0CSBHZ9','name':'蚌埠版','country':'CN','path':'蚌埠,蚌埠,安徽,中國權','timezone':'Asia/Shanghai','timezone_offset':'+08:00'},'now':{'text':'晴','code':'0','temperature':'24'},'last_update':'2017-09-14T09:10:00+08:00'}]}";
JSONObjectjson=JSON.parseObject(ss);
ObjectresultsJson=json.get("results");
JSONArrayarr=JSONArray.parseArray(resultsJson.toString());
System.out.println(arr.get(0));
}
}
6. java 中json對象轉為string類型
JSONObject對象不方便直接轉String對象,JSON容易轉換成其他對象,將待轉換的對象轉為jsonString類型然後可以很方便的轉換成其他類型JSON.parseObject(JSON.toJSONString(parseObject), String.class)
json包是fastjson,需要先下載
7. java json字元串轉對象 用什麼包
要想實現抄JSON和java對象之間的互轉,需要藉助第三方jar包,這里使用json-lib這個jar包,下載地址為:https://sourceforge.net/projects/json-lib/,json-lib需要commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar五個包的支持,
8. java中json字元串怎麼轉json對象
給你個思路:
1、將jsonstr轉為json對象 (這個可以用net.sf.json.JSONObject第三方包來實現)
2、根據refObj 這個key獲取{'existType':'exist','deptType':'emp','treeNodeType':'dept'},把獲取的這數據再轉為json.
3、將轉後的json根據existType就能取得值了 。
9. 怎樣從java後台獲取json字元串並轉換為json對象輸出
使用json-lib.jar這個工具
public String getJson(Object obj){
JSONObject json;
json = JSONObject.fromObject(obj);
return json.toString();
}
使用jquery來處理json
//轉換為json數據 datas可以用ajax從後台獲取上面getJson中的數據
var jsonDatas = eval("(" + datas + ")");
//循環遍歷數據
jQuery.each(jsonDatas, function(item) {
//循環
});
10. java 把json對象轉換成json字元串
java
有很多
1.
google
的gson
2.
阿里的fastjson
這個
是我用的,整個速度比較快,建議用這個,其他的不推薦