我可以使用javascript动态编辑JSON吗?

时间:2013-10-21 13:37:58

标签: javascript arrays json parameter-passing

您好我有一个json以下格式并保存为“Data.js”我想用我的one.js文件中的数组替换“示例文本”。

在Data.js

\"caption\":\"news item2\",\"text\":\"Sample text\",\"textFont\"

我想在one.js文件中删除带有我的新闻数组的第一个索引的“SAmple text”。

这可能。

Data.js文件如下所示

    prx.xdata = {"cc":6,
"data":{"template":{"id":"","stateid":""},"items":[{"left":11,"top":69,"width":298,"height":300,"type":"symbol","text":"","symbolid":1,"caption":"news container","visible":true,"scroll":"horizontal","scrollsnap":true,"pinchzoom":false,"lockdirection":true,"zindex":2001,"id":"box-6","groups":[],"hpos":"left","vpos":"top","wtype":"fixed","htype":"fixed","opacity":"1","propagateevents":false,"symbolstateid":"a8d0d79e-1921-4f7e-a229-75e5b1602881","bounce":false,"lib":"undefined"}]},"fonts":[],"grid":{"grid":{"size":50,"subdivisions":5},"gridsystem":{"portrait":{"col_number":2,"col_width":140,"gutter_width":20,"margins":10},"landscape":{"col_number":3,"col_width":140,"gutter_width":20,"margins":10}},"guides":{"custom":{"portrait":{"horizontal":[],"vertical":[]}}}},"_groupCounter":0,"savedColors":[],"iSelectedPage":0,"iSelectedTemplate":-1,"iSelectedSymbol":-1,"iSelectedState":0,"libraries":{"common":true,"icons":true,"iphone":true},"maxpageid":1,"maxsymbolid":1,"maxtemplateid":0,"project":{"statusbar":"1","navigationbar":"0","startscreenlandscape":"1","startscreen":"1","projectid":"11ac7f90-7e4c-4efe-a23c-d37aa70d7bcf"},"sort":{"pages":{"type":"default"},"templates":{"type":"default"},"containers":{"type":"default"}},"variables":{},"v5":true,"pages":[{"orientation":"portrait","statusbar":"1","navigationbar":"0","id":1,"actions":[],"title":"Screen 1","states":[{"data":"[{\"left\":11,\"top\":69,\"width\":298,\"height\":300,\"type\":\"symbol\",\"text\":\"\",\"symbolid\":1,\"caption\":\"news container\",\"visible\":true,\"scroll\":\"horizontal\",\"scrollsnap\":true,\"pinchzoom\":false,\"lockdirection\":true,\"zindex\":2001,\"id\":\"box-6\",\"groups\":[],\"hpos\":\"left\",\"vpos\":\"top\",\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\",\"propagateevents\":false,\"symbolstateid\":\"a8d0d79e-1921-4f7e-a229-75e5b1602881\",\"bounce\":false,\"lib\":\"undefined\"}]","template":{"id":"","stateid":""},"title":"Default State","history":[],"background":"none","id":"6b3b49d1-c302-4035-a254-89157ce10277"}],"sort":0}],"templates":[],"symbols":[{"id":1,"title":"news container","states":[{"title":"Default State","background":"none","data":"[{\"name\":\"text\",\"type\":\"text\",\"lib\":\"common\",\"caption\":\"newsitem1\",\"text\":\"Sample text\",\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",\"backgroundColor\":\"none\",\"width\":292,\"height\":257,\"textProperties\":[],\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":8,\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",\"visible\":true,\"id\":\"box-2\",\"groups\":[],\"zindex\":2001,\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\"},{\"name\":\"text\",\"type\":\"text\",\"lib\":\"common\",\"caption\":\"news item2\",\"text\":\"Sample text\",\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",\"backgroundColor\":\"none\",\"width\":301,\"height\":257,\"textProperties\":[],\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":300,\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",\"visible\":true,\"id\":\"box-3\",\"groups\":[],\"zindex\":2002,\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\"},{\"name\":\"text\",\"type\":\"text\",\"lib\":\"common\",\"caption\":\"news item3\",\"text\":\"Sample text\",\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",\"backgroundColor\":\"none\",\"width\":292,\"height\":257,\"textProperties\":[],\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":601,\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",\"visible\":true,\"id\":\"box-4\",\"groups\":[],\"zindex\":2003,\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\"},{\"name\":\"text\",\"type\":\"text\",\"lib\":\"common\",\"caption\":\"news item4\",\"text\":\"Sample text\",\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",\"backgroundColor\":\"none\",\"width\":298,\"height\":257,\"textProperties\":[],\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":901,\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",\"visible\":true,\"id\":\"box-5\",\"groups\":[],\"zindex\":2004,\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\"}]","history":[],"dimensions":["1200","300"],"id":"a8d0d79e-1921-4f7e-a229-75e5b1602881"}],"sort":0,"customguides":{"horizontal":[],"vertical":[]},"grid":{"col_number":3,"col_width":80,"gutter_width":20,"margins":10}}]};

prx.xdata_str = JSON.stringify(prx.xdata);

1 个答案:

答案 0 :(得分:0)

你必须使用正则表达式并替换

像这样:

var newFile = prx.xdata_str.replace(/Sample/g, "");

然后将该字符串发送到您的服务器并保存。