0

嗨,我有一个类似以下格式的 Json。我想用text:"Sample Text" js 文件中我的数组中的值替换示例文本的值。

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]);
    }

}
4

1 回答 1

-1

var x = "{a: 'a', b: 'b'}"这是一个 json 字符串

var o = eval('"' + x + '"')o 是 json 对象,已解析。

于 2013-10-22T07:13:52.160 回答