嗨我有一个像Json一样的格式。我想用{s}文件中My array的值替换text:"Sample Text"
示例文本的值。
JSON格式
prx.xdata = {"cc":6,
"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);
我想替换此路径中“示例文本”的值
symbols[0].states[0].data[0].text with value from News array.
问题在这里 symbols [0] .states [0] .data这个数据是另一个JSON,因此无法解析该数据。
请查看我的Js代码
function yourJsFunction(arr){
var b=arr.toString().split(',');
var News =new Array();
News=b;
var jsondata = JSON.parse(prx.xdata);
//alert(jsondata.symbols[0].states[0].data);
var data=new Array();
data=jsondata.symbols[0].states[0].data;
// alert(data);
var newData=JSON.parse(data);
newData[0].text=News[0];
newData[1].text=News[1];
newData[2].text=News[2];
newData[3].text=News[3];
alert(newData[0].text);
//This alert showing my updated json.But when i click ok in alert the webview showing previous Json
jsondata.symbols[0].states[0].data= JSON.stringify(newData);
prx.xdata = JSON.stringify(jsondata);
for(var i=0;i<arr.length;i++){
// document.write(arr[i]);
}
}
答案 0 :(得分:-1)
var x = "{a: 'a', b: 'b'}"
这是一个json字符串
var o = eval('"' + x + '"')
o是json对象,已解析。