如何把json对象转换成字符串 如何把json对象转换成字符串
\u5982\u4f55\u5c06 javascript \u5bf9\u8c61\u8f6c\u6362\u6210 json\u5b57\u7b26\u4e32JSON\uff08JavaScript Object Notation\uff09 \u662fJavaScript\u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u4e2a\u5b50\u96c6\u3002\u6b63\u56e0JSON\u662fJavaScript\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u6240\u4ee5\u5b83\u53ef\u6e05\u6670\u7684\u8fd0\u7528\u4e8e\u6b64\u8bed\u8a00\u4e2d\u3002
eval\u51fd\u6570 JSON\u6587\u672c\u8f6c\u6362\u4e3a\u5bf9\u8c61
\u4e3a\u4e86\u5c06JSON\u6587\u672c\u8f6c\u6362\u4e3a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528eval\u51fd\u6570\u3002eval\u51fd\u6570\u8c03\u7528JavaScript\u7f16\u8f91\u5668\u3002\u7531\u4e8eJSON\u662fJavaScript\u7684\u5b50\u96c6\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5c06\u6b63\u786e\u7684\u89e3\u6790\u6587\u672c\u5e76\u4ea7\u751f\u5bf9\u8c61\u7ed3\u6784\u3002\u6587\u672c\u5fc5\u987b\u62ec\u5728\u62ec\u53f7\u4e2d\u907f\u514d\u4ea7\u751fJavaScript\u7684\u8bed\u6cd5\u6b67\u4e49\u3002
var obj = eval('(' + JSONTest + ')');
eval\u51fd\u6570\u975e\u5e38\u5feb\u901f\u3002\u5b83\u53ef\u4ee5\u7f16\u8bd1\u6267\u884c\u4efb\u4f55JavaScript\u7a0b\u5e8f\uff0c\u56e0\u6b64\u4ea7\u751f\u4e86\u5b89\u5168\u6027\u95ee\u9898\u3002\u5f53\u4f7f\u7528\u53ef\u4fe1\u4efb\u4e0e\u5b8c\u5584\u7684\u6e90\u4ee3\u7801\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528eval\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u5b89\u5168\u7684\u89e3\u6790JSON\u6587\u672c\u3002\u4f7f\u7528XmlHttp\u7684web\u5e94\u7528\uff0c\u9875\u9762\u4e4b\u95f4\u7684\u901a\u8baf\u53ea\u5141\u8bb8\u540c\u6e90\uff0c\u56e0\u6b64\u662f\u53ef\u4ee5\u4fe1\u4efb\u7684\u3002\u4f46\u8fd9\u5374\u4e0d\u662f\u5b8c\u5584\u7684\u3002\u5982\u679c\u670d\u52a1\u5668\u6ca1\u6709\u4e25\u8c28\u7684JSON\u7f16\u7801\uff0c\u6216\u8005\u6ca1\u6709\u4e25\u683c\u7684\u8f93\u5165\u9a8c\u8bc1\uff0c\u90a3\u4e48\u53ef\u80fd\u4f20\u9001\u5305\u62ec\u5371\u9669\u811a\u672c\u7684\u65e0\u6548JSON\u6587\u672c\u3002eval\u51fd\u6570\u5c06\u6267\u884c\u6076\u610f\u7684\u811a\u672c\u3002
JSON\u89e3\u91ca\u5668 JSON.parse\u3001JSON.stringify
\u4f7f\u7528JSON\u89e3\u6790\u5668\u53ef\u4ee5\u9632\u6b62\u50cfeval\u51fd\u6570\u8f6c\u6362JSON\u6587\u672c\u4e3a\u5bf9\u8c61\u90a3\u6837\u7684\u5b89\u5168\u9690\u60a3\u3002JSON\u89e3\u6790\u5668\u53ea\u80fd\u8fa8\u8bc6JSON\u6587\u672c\uff0c\u62d2\u7edd\u6240\u6709\u811a\u672c\u3002\u63d0\u4f9b\u4e86\u672c\u5730JSON\u652f\u6301\u7684\u6d4f\u89c8\u5668\u7684JSON\u89e3\u6790\u5668\u5c06\u8fdc\u5feb\u4e8eeval\u51fd\u6570\u3002
\u76ee\u524d\uff0cFirefox\u3001Opera\u3001IE8\u4ee5\u4e0a\u7248\u672c\u4e5f\u63d0\u4f9b\u4e86\u672c\u5730JSON\u652f\u6301\u3002\u5176\u4e2d\uff0cJSON\u89e3\u91ca\u5668\u63d0\u4f9b\u7684\u51fd\u6570\u6709\uff1aJSON.parse\u3001JSON.stringify\u3002
\u5bf9\u4e8e\u90a3\u4e9b\u5e76\u4e0d\u63d0\u4f9b\u672c\u5730JSON\u652f\u6301\u7684\u6d4f\u89c8\u5668\u53ef\u4ee5\u5f15\u5165\u811a\u672cjson2.js\uff0c\u6765\u5b9e\u73b0JSON\u8f6c\u6362\u529f\u80fd\u3002json2.js\u811a\u672c\u53ef\u5230https://github.com/douglascrockford/JSON-js/blob/master/json2.js\u9875\u9762\u4e0b\u8f7d\u3002
JSON.parse \u51fd\u6570
\u5c06JSON\u6587\u672c\u8f6c\u6362\u4e3a\u5bf9\u8c61\u3002
JSON.parse(text[, reviver])
\u53c2\u6570
text
\u5fc5\u9009\u9879\u3002\u8981\u8f6c\u6362\u4e3a\u5bf9\u8c61\u7684JSON\u6587\u672c\u3002
reviver
\u53ef\u9009\u9879\u3002\u8be5\u53c2\u6570\u662f\u4e2a\u66ff\u6362\u51fd\u6570\u3002\u5728\u8f6c\u6362\u4e2d\uff0c\u904d\u5386\u7684\u6bcf\u4e2a\u8282\u70b9\uff0c\u90fd\u5c06\u6267\u884c\u8be5\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c06\u66ff\u4ee3\u8f6c\u6362\u7ed3\u679c\u7684\u76f8\u5e94\u8282\u70b9\u503c\u3002
JSON.stringify \u51fd\u6570
\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u6587\u672c\u3002
JSON.stringify(value[, replacer[, space]])
\u53c2\u6570
text
\u5fc5\u9009\u9879\u3002\u8981\u8f6c\u6362\u4e3aJSON\u6587\u672c\u7684\u5bf9\u8c61\u3002
reviver
\u53ef\u9009\u9879\u3002\u8be5\u53c2\u6570\u662f\u4e2a\u66ff\u6362\u51fd\u6570\u3002\u5728\u8f6c\u6362\u4e2d\uff0c\u904d\u5386\u7684\u6bcf\u4e2a\u8282\u70b9\uff0c\u90fd\u5c06\u6267\u884c\u8be5\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c06\u66ff\u4ee3\u8f6c\u6362\u7ed3\u679c\u7684\u76f8\u5e94\u8282\u70b9\u503c\u3002
space
\u53ef\u9009\u9879\u3002\u683c\u5f0f\u5316\u8f93\u51faJSON\u6587\u672c\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u91cf\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\u8be5\u53c2\u6570\u5c06\u4e0d\u4f1a\u683c\u5f0f\u5316\u8f93\u51fa\u3002
\u53c2\u6570reviver\u7684\u59d4\u6258\u7c7b\u578b
reviver(key, value)
reviver\u51fd\u6570\u7684\u4e2d\u7684this\u662f\u5f53\u524d\u6240\u904d\u5386\u5230\u7684\u8282\u70b9\u7684\u7236\u8282\u70b9\u3002\u5f53\u6240\u904d\u5386\u7684\u662f\u6839\u8282\u70b9\u7684\u65f6\u5019\uff0c\u7236\u8282\u70b9\u662f\u4e2aObject\u5bf9\u8c61\uff0c\u6839\u8282\u70b9\u662f\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u5c5e\u6027\u540d\u662f\u7a7a\u5b57\u7b26\u4e32\u3002
\u53c2\u6570
key
\u5f53\u7236\u8282\u70b9\u662f\u6570\u7ec4Object\uff0ckey\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u5426\u5219key\u4e3aObject\u5c5e\u6027\u540d\u3002
value
\u8282\u70b9\u503c\u3002
\u6ce8\uff1aJSON\u4e0d\u652f\u6301\u5faa\u73af\u6570\u636e\u7ed3\u6784\u3002
\u3000\u3000JSON.parse\u3001JSON.stringify\u4f7f\u7528\u793a\u4f8b
\u3000\u3000[javascript] view plaincopy
\u3000\u3000
\u3000\u3000function InventoryItem(parm) {
\u3000\u3000this.Product = parm.Product
\u3000\u3000this.Quantity = parm.Quantity;
\u3000\u3000this.Price = parm.Price;
\u3000\u3000this.Type = parm.Type;
\u3000\u3000this.Total = function() {
\u3000\u3000return this.Price * this.Quantity;
\u3000\u3000}
\u3000\u3000}
\u3000\u3000
\u3000\u3000function Inventory(parm) {
\u3000\u3000this.Date = parm.Date;
\u3000\u3000this.Item = parm.Item;
\u3000\u3000this.Type = parm.Type;
\u3000\u3000this.Total = function() {
\u3000\u3000var count = 0;
\u3000\u3000for (var key in this.Item) {
\u3000\u3000count += this.Item[key].Total();
\u3000\u3000}
\u3000\u3000return count;
\u3000\u3000}
\u3000\u3000}
\u3000\u3000
\u3000\u3000var inventoryJSONText = "{\"Date\":\"2000-01-01\",\"Item\":[{\"Product\":\"ProductOne\",\"Quantity\":\"10\"
\u3000\u3000,\"Price\":\"10\",\"Type\":\"InventoryItem\"},{\"Product\":\"ProductTwo\",\"Quantity\":\"100\",\"Price\":\"20\"
\u3000\u3000,\"Type\":\"InventoryItem\"}],\"Type\":\"Inventory\"}";
\u3000\u3000
\u3000\u3000//\u5c06JSON\u6587\u672c\u8f6c\u6362\u4e3a\u5bf9\u8c61\uff0c\u5e76\u5c06\u4e00\u822c\u7c7b\u7684\u5b9e\u4f8b\u6539\u6210\u4f2a\u7c7b\u7684\u5b9e\u4f8b\u3002
\u3000\u3000var inventoryObject = JSON.parse(inventoryJSONText, function(key, value) {
\u3000\u3000var type;
\u3000\u3000if (value && typeof value === 'object') {
\u3000\u3000type = value.Type;
\u3000\u3000if (typeof type === 'string' && typeof window[type] === 'function') {
\u3000\u3000return new (window[type])(value);
\u3000\u3000}
\u3000\u3000}
\u3000\u3000return value;
\u3000\u3000});
\u3000\u3000
\u3000\u3000//\u8f93\u51fa\u8f6c\u6362\u540e\u5bf9\u8c61\u7684\u4fe1\u606f\u3002
\u3000\u3000var output = "Product\t\tQuantity\tPrice\tTotal\n";
\u3000\u3000for (var key in inventoryObject.Item) {
\u3000\u3000var item = inventoryObject.Item[key];
\u3000\u3000output += item.Product + "\t" + item.Quantity + "\t\t"
\u3000\u3000+ item.Price + "\t" + item.Total() + "\n";
\u3000\u3000}
\u3000\u3000output += "\t\t\t\t\t" + inventoryObject.Total();
\u3000\u3000alert(output);
\u3000\u3000
\u3000\u3000//\u518d\u5c06inventoryObject\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6587\u672c\u3002
\u3000\u3000var inventoryJSONTextAgain = JSON.stringify(inventoryObject, null, 3);
\u3000\u3000alert(inventoryJSONTextAgain);
\u3000\u3000
1.\u7b80\u5355\u7684\u89e3\u6790json\u5b57\u7b26\u4e32
\u9996\u5148\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3ajson\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u89e3\u6790json\u5bf9\u8c61\uff0c\u8fc7\u7a0b\u5982\u4e0b\u3002
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
[java] view plain copy
\u6839\u636ejson\u4e2d\u7684\u952e\u5f97\u5230\u5b83\u7684\u503c
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");
2.\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3ajava\u5bf9\u8c61
\u540c\u6837\u5148\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3ajson\u5bf9\u8c61\uff0c\u518d\u5c06json\u5bf9\u8c61\u8f6c\u6362\u4e3ajava\u5bf9\u8c61\uff0c\u5982\u4e0b\u6240\u793a\u3002
JSONObject obj = new JSONObject().fromObject(jsonStr);//\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3ajson\u5bf9\u8c61
\u5c06json\u5bf9\u8c61\u8f6c\u6362\u4e3ajava\u5bf9\u8c61
Person jb = (Person)JSONObject.toBean(obj,Person.class);//\u5c06\u5efajson\u5bf9\u8c61\u8f6c\u6362\u4e3aPerson\u5bf9\u8c61
3.\u5c06java\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32
\u5148\u5c06java\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u5bf9\u8c61\uff0c\u5728\u5c06json\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32
JSONObject json = JSONObject.fromObject(obj);//\u5c06java\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u5bf9\u8c61
String str = json.toString();//\u5c06json\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32
\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a
[java] view plain copy
package baz.parse;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import baz.bean.Person;
public class ParseJson {
private String jsonStr;
public ParseJson() {
}
首先将json字符串转换为json对象,然后再解析json对象,过程如下。
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
[java] view plain copy
<pre></pre><span style="white-space:pre"></span>
<pre></pre>
根据json中的键得到它的值
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");
2.将json字符串转换为java对象
同样先将json字符串转换为json对象,再将json对象转换为java对象,如下所示。
JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象
将json对象转换为java对象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//将建json对象转换为Person对象
3.将java对象转换为json字符串
先将java对象转换为json对象,在将json对象转换为json字符串
JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
String str = json.toString();//将json对象转换为字符串
绛旓細Obj2str: function(o) { if (o == undefined) { return "";} var r = [];if (typeof o == "string") return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";if (...
绛旓細using System.Runtime.Serialization.Json;using System.Web.Script.Serialization;...public static string GetJson(object obj){ JavaScriptSerializer serializer = new JavaScriptSerializer();return serializer.Serialize(obj);}
绛旓細//鎺ユ敹{}瀵硅薄锛屾澶勬帴鏀舵暟缁勫璞′細鏈夊紓甯 if(jsonStr.indexOf("[") != -1){ jsonStr = jsonStr.replace("[", "");} if(jsonStr.indexOf("]") != -1){ jsonStr = jsonStr.replace("]", "");} JSONObject obj = new JSONObject().fromObject(jsonStr);//灏json瀛楃涓茶浆鎹涓...
绛旓細json 瀛楃涓鎸囩殑灏辨槸 json 鏈韩锛岀敱浜 JavaScript 鏄急绫诲瀷璇█锛屾墍浠ユ病鏈夊姙娉曠洿鎺ヤ紶閫掑璞℃垨鏄暟缁勶紝鎵浠ユ彁鍑轰簡 json 鏍煎紡鐨勫瓧绗︿覆锛岀敤浜庢暟鎹紶閫;ajax 涓紝鎴戜滑鑷繁鎷兼帴鐨勬槸涓涓JSON瀵硅薄锛屽洜涓哄畠鏄棤鏁版嵁绫诲瀷鐨勶紝鎵浠 JS 鏍规嵁鍏舵牸寮忛粯璁ゅ叾鏄璞★紝瑕佹槸寰鍚庡彴鍙戯紝瑕佸厛鎶婂畠杞崲鎴 JSON瀛楃 銆備粠 ajax...
绛旓細鍙互浣跨敤json2鏉ュ疄鐜帮紝寮曞叆涓媕son2.js,鐒跺悗浣跨敤JSON.stringify(obj);
绛旓細JSON.stringify 鎶婁竴涓瀵硅薄杞崲鎴恓son瀛楃涓 JSON.parse 鎶婁竴涓猨son瀛楃涓茶В鏋鎴愬璞 --- 鏃х増娴忚鍣ㄥ紩鍏 json2.js 鍙互鏀寔杩欎簺鏂规硶
绛旓細鏍规嵁浣犵殑json鏁版嵁锛屽彲浠ュ儚涓嬮潰鏂瑰紡杩涜杞崲锛氳皟鐢–ustom 绫讳腑鐨 DeJson鏂规硶 浼犲叆json瀛楃涓 杩斿洖瀵硅薄 杩介棶锛氬鏋滅収杩欐牱瀹氫箟锛宑ustom姘歌繙灏卞彧鏈変袱涓猚hapter锛屽鏋渏son涓湁list3锛屽氨涓嶈浜嗐傝拷绛旓細濡傛灉涓嶆槸鍥哄畾鐨勭被锛屽彲浠ヤ娇鐢∟ewtonsoft涓彁渚涚殑JObject瀵硅薄,浣嗘槸濡傛灉璇翠綘杩樻槸甯屾湜鍙嶅簭鍒楀寲鎴愬璞鐨勮瘽锛岄偅涔 浣...
绛旓細浠g爜涓殑Id绫诲瀷鏄睘浜嶯umber绫诲瀷鐨勶紝鍦杞崲杩囩▼涓彂鐢熶簡绮惧害澶辩湡銆傚鏋淚d鏄被鍨嬫槸String,value鍊煎皢娌℃湁浠讳綍闂銆
绛旓細閫氳繃`parseObject`鏂规硶锛屽彲浠ュ皢JSON瀛楃涓茶浆鎹涓篔avaBean瀹炰緥锛歚YourClass bean = JSON.parseObject(jsonString, YourClass.class);`4.鐩稿弽锛屽鏋滈渶瑕佸皢JavaBean杞崲涓篔SON锛屼娇鐢╜toJSONString`鏂规硶锛歚String jsonString = bean.toJSONString();`5.瀵逛簬Map锛屽彲浠ヤ娇鐢╜ObjectMapper`灏咼SON瀛楃涓叉槧灏勪负Map瀵...
绛旓細JSON鏄瀛楃涓銆侸SON鏄疛S瀵硅薄鐨勫瓧绗︿覆琛ㄧず娉曪紝瀹冧娇鐢ㄦ枃鏈〃绀轰竴涓狫S瀵硅薄鐨勪俊鎭紝鏈川鏄竴涓瓧绗︿覆銆侸SON鏄熀浜庢枃鏈瓧绗︿覆锛堢浉瀵逛簬浜岃繘鍒讹級鐨勬枃妗f牸寮忥紝鍜孒TML銆乆ML绫讳技銆傚湪寰堝鎯呭喌涓嬶紝JSON鍙互鐢ㄦ潵鎻忚堪鐗瑰畾鏁版嵁缁撴瀯鐨勫疄渚嬨