请问json如何追加内容,并且修改,和删除操作。急~ 将json格式的数据输出到html页面,急急急~
EXTJS \u4e2djson\u4f1a\u628a\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u4e2a\u7a7a\u683c\u53d8\u4e3a\u4e00\u4e2a\uff0c\u600e\u6837\u8bbe\u7f6e\u624d\u80fd\u8ba9\u5b83\u4e0d\u6539\u53d8\u5b57\u7b26\u4e32\u4e2d\u7a7a\u683c\u7684\u4e2a\u6570\uff1f\u6025~~~json\u5e94\u8be5\u4e0d\u4f1a\u5904\u7406\u7a7a\u683c\u7684\uff0chtml\u624d\u4f1a\u5c06\u591a\u4e2a\u7a7a\u683c\u5904\u7406\u6210\u4e00\u4e2a\u7a7a\u683c\u3002
\u8bf7\u8bd5\u7740\u5728\u8f93\u51fa\u7684\u5185\u5bb9\u5916\u9762\u52a0\u4e0a
\u524d\u53f0\u5728js\u904d\u5386\u4e3e\u4e2a\u4f8b\u5b50
$.get('url',function(josn){ for(json.data in arr){ alert(arr.expect); }});
json追加内容并且修改和删除操作示例:
1、增加:
myObj.user='我是新增的用户-小明';
x +="<h1>增加后的数据</h1>"forin();
2、修改:
myObj.name= "我的网站";
x +="<h1>修改后的数据</h1>"forin();
3、删除:
delete myObj.sites;
x +="<h1>删除后的数据</h1>"forin();
扩展资料
JS动态动态创建JSON数据字符串,并且可以删除添加修改
<script type="text/javascript">
//添加或者修改json数据
function setJson(jsonStr,name,value)
{
if(!jsonStr)jsonStr="{}";
var jsonObj = JSON.parse(jsonStr);
jsonObj[name] = value;
return JSON.stringify(jsonObj)
}
//删除数据
function deleteJson(jsonStr,name)
{
if(!jsonStr)return null;
var jsonObj = JSON.parse(jsonStr);
delete jsonObj[name];
return JSON.stringify(jsonObj)
}
//生成测试
var myjsonStr = setJson(null,"name","aaa");
alert(myjsonStr);
//添加测试
myjsonStr = setJson(myjsonStr,"age",18);
alert(myjsonStr);
//修改测试
myjsonStr = setJson(myjsonStr,"age",20);
alert(myjsonStr);
//删除测试
myjsonStr = deleteJson(myjsonStr,"age");
alert(myjsonStr);
</script>
使用方法
var str = '[{"name":"aa","age":"23"},{"name":"bb","age":"24"},{"name":"cc","age":"25"}]';
var array=JSON.parse(str);
array.push({"name":"dd","age":"25"});
//修改
var obj=array.firstOrDefault(
function(x){
return x.name=='bb';
}
);
obj.age=25;
//删除
array.delete(
function(x){
return x.name=='cc';
}
);
//转为json字符串
str=JSON.stringify(array);
js代码文件js.js
//----------
Array.prototype.firstOrDefault = function (predicate) {
var first = null;
if (typeof predicate == "undefined") {
if (this.length > 0)
first = this[0];
} else if (typeof predicate == "function") {
for (var i = 0; i < this.length; i++) {
if (predicate(this[i])) {
first = this[i];
break;
}
}
} else {
throw new Error("predicate 类型必须为function");
}
return first;
};
Array.prototype.delete = function (predicate) {
if (typeof predicate == "function") {
var obj = this.firstOrDefault(predicate);
if (obj) {
delete obj;
}
} else {
throw new Error("predicate 类型必须为function");
}
};
额,这个你先获取json 然后,遍历啊
绛旓細json杩藉姞鍐呭骞朵笖淇敼鍜屽垹闄ゆ搷浣滅ず渚嬶細1銆佸鍔狅細myObj.user='鎴戞槸鏂板鐨勭敤鎴-灏忔槑';x+="澧炲姞鍚庣殑鏁版嵁"forin();2銆佷慨鏀癸細myObj.name="鎴戠殑缃戠珯";x+="淇敼鍚庣殑鏁版嵁"forin();3銆佸垹闄わ細deletemyObj.sites;x+="鍒犻櫎鍚庣殑鏁版嵁"forin();
绛旓細json杩藉姞鍐呭骞朵笖淇敼鍜屽垹闄ゆ搷浣滅ず渚嬶細1銆佸鍔狅細myObj.user='鎴戞槸鏂板鐨勭敤鎴-灏忔槑';x +="澧炲姞鍚庣殑鏁版嵁"forin();2銆佷慨鏀癸細myObj.name= "鎴戠殑缃戠珯";x +="淇敼鍚庣殑鏁版嵁"forin();3銆佸垹闄わ細delete myObj.sites;x +="鍒犻櫎鍚庣殑鏁版嵁"forin();...
绛旓細1銆侀鍏堬紝鎵撳紑html缂栬緫鍣紝鏂板缓html鏂囦欢锛屼緥濡傦細index.html銆2銆佷互杩藉姞name鍏冪礌涓轰緥锛屽湪index.html鐨勬爣绛句腑锛岃緭鍏s浠g爜锛歷ar names = ['Alice', 'Bob'];var json = [];for (var i = 0; i < names.length; i++) { json[i] = {'name' : names[i]};} document.body.innerText = ...
绛旓細json.array1=["2","4"];// 澧炲姞涓涓柊灞炴э紝姝ゅ睘鎬ф槸鏁扮粍 json.array1[json.array1.length]='6';// 鏁扮粍杩藉姞涓涓厓绱 alert(json.array1)
绛旓細file_get_contents("test.json");$jsonStr = str_replace("var goodsData=[","[",$jsonStr);$jsonStr = str_replace("];","]",$jsonStr);$jsonData = json_decode($jsonStr,true);$data = array();$data['a'] = 'test';$data['b'] = '12133';array_push($json...
绛旓細var newJson='{"name":"liubei","sex":"鐢"}';j.push(JSON.parse(newJson));console.log(j);閬嶅巻杩欎釜j锛歠or(var i=0;i<j.length;i++){ var json = j[i];console.log(json.name,json.sex);} //鎴栬呰繖鏍凤細j.forEach(function(json){ console.log(json);console.log(json.name,...
绛旓細import jsonreaded = json.load(open('jsonsource.dat', 'r'))json.dump(readed, open('newjsonfile.dat', 'w'))
绛旓細1棣栧厛鍦json璇硶涓槸娌℃湁娉ㄩ噴璇硶鐨勶紝娣诲姞娉ㄩ噴鎴栬**鍦↗SON鏂囦欢涓槸涓嶅厑璁哥殑锛孞SON鏈変袱绉嶆暟鎹粨鏋勫悕绉板煎鐨勯泦鍚坘ey value鏍峰紡鍊肩殑鏈夊簭鍒楄〃灏辨槸Array鑰屽湪JSON鐨勬枃妗d腑璇存槑鍙鏄笉绗﹀悎涓婇潰涓ょ缁撴瀯鐨勩備笉鑳藉姞鍦╭uotdependenciesquot閲岋紝浣嗘槸鍙互鍔犱负packagejson鐨勬渶椤跺眰椤 quotquot quotThis is comment 1...
绛旓細= { "name" : $('#names').val(),"value" : $('#values').val()} jsonarray.push(arr);//2銆乿ar json={};// 瀹氫箟涓涓猨son瀵硅薄 json.array1=["2","4"];// 澧炲姞涓涓柊灞炴э紝姝ゅ睘鎬ф槸鏁扮粍 json.array1[json.array1.length]='6';// 鏁扮粍杩藉姞涓涓厓绱 alert(json.array1)...
绛旓細鍙互鍦ㄥ紑澶存彃鍏, 鎴杩藉姞鍦ㄧ粨灏, json灏辨槸JavaScript鐨勮娉, 鍜孞S瀹屽叏涓鏍风殑渚嬪瓙涓//娉ㄩ噴/*娉ㄩ噴, 娉ㄦ剰娉ㄩ噴蹇呴』鐢ㄦ崲琛岄殧寮, 灏辨槸JS鐨勮娉, 涓嶇劧涓琛岄兘琚敞閲婃帀浜*/ {a:1,b:2,c:[3,4,5]} 杩藉姞鍦ㄦ渶鍚庝篃鏄竴鏍风殑, 浣嗘槸涓嶈兘鍔犲湪涓棿, 鍜屼綘涔烰S浠g爜璇硶涓鏍风殑 鏈鍚, JSON鍜宩avascript鏈夊尯鍒悧...