【问题标题】:How can i parse a JSON inside another JSON together?我如何一起解析另一个 JSON 中的 JSON?
【发布时间】:2013-10-22 07:09:12
【问题描述】:

您好,我有一个类似以下格式的 Json。我想用 js 文件中我的数组中的值替换 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]);
    }

}

【问题讨论】:

  • 这叫做 JInception
  • JSon 的开始。开个玩笑。
  • @Navi 你在prx.xdata 中有很多未闭合的字符串......你的字符串没有正确转义......转到jshint.com 并复制粘贴你的整个prx.xdata 你会看到......

标签: javascript arrays json parsing


【解决方案1】:

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

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

【讨论】:

  • 想说什么??我是javascript新手,请解释一下
  • 我想告诉你如何将 json 字符串解析为 json 对象。然后您可以修改“文本”字段并将其字符串化。
  • @sg3s 他想替换 json 字符串中的一个属性,所以我虽然 parse-modify-stringify 是他想要的。他似乎知道stringify,然后我告诉他如何解析json字符串。
猜你喜欢
  • 1970-01-01
  • 2013-10-22
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 2017-10-17
相关资源
最近更新 更多