将字符串(带结构对象)转换为对象

时间:2013-10-29 15:18:53

标签: javascript

我有一个json信息,我想用它来创建像这样的对象

var legends = {

    "Altimetry" : {
        0 : {
            legenrowname : "0m - 250m    ",
            fillcolor : "005500",
            bordercolor : ""
        },
        1 : {
            legenrowname : "250m - 1000m ",
            fillcolor : "ffff7f",
            bordercolor : ""
        },
        2 : {
            legenrowname : "1000m - 5000m ",
            fillcolor : "B47811",
            bordercolor : ""
        },
        3 : {
            legenrowname : "+ 5000m       ",
            fillcolor : "482400",
            bordercolor : ""
        }
    }
};

alert(legends); //the result is this:
Object []

它被认为是一个对象(这就是我下一步所需要的)

现在,我正在尝试使用legends2

动态创建它
var dataL = ${dataJsonLegends};
var legends2 ="";
var lengthL = dataL.length;
$.each(dataL, function(i, item){
legendName = item.legendname;
legends2= '"' + legendName + '":{';
dataR = item.rows;
var lengthR = dataR.length;
$.each(dataR, function(j, item2){
    id=item2.uid;
    rowname=item2.legendrowname;
    fillcolor=item2.fillcolor;
    rowobject = id + ' : { legenrowname:"' + rowname + '", fillcolor:"' + fillcolor + '"}';
    if(j!=lengthR-1) rowobject = rowobject + ",";

    legends2= legends2+ rowobject;
});
legends2= legends2+ '}';
if(i!=lengthL-1) legends2= legends2+ ",";
});
alert(legends2); //the result is this:

"Altimetry" : { 0 : {       legenrowname : "0m - 250m    ",
            fillcolor : "005500",
            bordercolor : ""
    },1 : {
            legenrowname : "250m - 1000m ",
            fillcolor : "ffff7f",
            bordercolor : ""
    },2 : {
            legenrowname : "1000m - 5000m ",
            fillcolor : "B47811",
            bordercolor : ""
    },3 : {
            legenrowname : "+ 5000m       ",
            fillcolor : "482400",
            bordercolor : ""
    }}

legends2值是我的预期,但我希望它被识别为一个对象,而不是一个具有对象结构的字符串。

我尝试了很多东西来解决它。 JSON.parseeval等等......但没人工作。

¿有人可以帮忙或给我一个提示吗?

编辑:传入json

我发布了我的json对象(dataL),用于创建我需要的结构。

[{"uid":1,"legendname":"Altimetry","legend_description":null,"rows":[{"uid":2,"legendrowname":"250m - 1000m","fillcolor":"ffff7f","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null},{"uid":4,"legendrowname":"+ 5000m","fillcolor":"482400","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null},{"uid":1,"legendrowname":"0m - 250m","fillcolor":"005500","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null},{"uid":3,"legendrowname":"1000m - 5000m","fillcolor":"B47811","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null}],"layer":{"id":1,"description":"Altimetry","geoserver":"/geoserver/Sopcawind/wms","name":"Sopcawind:srtm","transparent":true,"format":"image/png","viewparams":"","isbaselayer":false,"opacity":0.5,"color":"#2b383b","visibility":false,"groupid":1,"infocontrol":true,"geometry":"","featuretype":"","srs":"","extradata":"","legends":null,"project":null}}];

Edited2: 我试图使用代码,但我真的不需要一个json对象。我使用json对象创建具有此结构的另一个基本对象:

//loop legends
    legends={"legendname": {
    //loop rows
        row.id : {
            legenrowname : row.legendrowname,
            fillcolor : row.fillcolor,
            bordercolor : row.bordercolor
        }   
    }}

2 个答案:

答案 0 :(得分:0)

你不应该这么复杂,你帖子顶部的Json是无效的。 也许这是你想要的东西?

{
    "Altimetry": [
        {
            "legenrowname": "0m - 250m    ",
            "fillcolor": "005500",
            "bordercolor": ""
        },
        {
            "legenrowname": "250m - 1000m ",
            "fillcolor": "ffff7f",
            "bordercolor": ""
        },
        {
            "legenrowname": "1000m - 5000m ",
            "fillcolor": "B47811",
            "bordercolor": ""
        },
        {
            "legenrowname": "+ 5000m       ",
            "fillcolor": "482400",
            "bordercolor": ""
        }
    ]
}

检查传入的JSON是否为http://jsonlint.com/,如果它有效。 你自己生成了JSON吗? 执行console.log(jsonString)并将其粘贴到jsonlint。

使用有效的JSON,您可以使用JSON.parse(jsonString) - 不要添加任何额外的括号 比如var dataL = ${dataJsonLegends};

现在看起来你正在循环遍历响应字符串中的每个字符

修改

您更新的JSON是有效的,这就是我处理它的方式:

var json = [{"uid":1,"legendname":"Altimetry","legend_description":null,"rows":[{"uid":2,"legendrowname":"250m - 1000m","fillcolor":"ffff7f","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null},{"uid":4,"legendrowname":"+ 5000m","fillcolor":"482400","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null},{"uid":1,"legendrowname":"0m - 250m","fillcolor":"005500","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null},{"uid":3,"legendrowname":"1000m - 5000m","fillcolor":"B47811","bordercolor":null,"rast_value":null,"fillcolorrgb":null,"bordercolorrgb":null,"legendrow_description":null,"legend":null}],"layer":{"id":1,"description":"Altimetry","geoserver":"/geoserver/Sopcawind/wms","name":"Sopcawind:srtm","transparent":true,"format":"image/png","viewparams":"","isbaselayer":false,"opacity":0.5,"color":"#2b383b","visibility":false,"groupid":1,"infocontrol":true,"geometry":"","featuretype":"","srs":"","extradata":"","legends":null,"project":null}}];


for(i = 0; i<json.length; i++) {
    console.log("new array item");
    console.log(json[i].uid);
    console.log(json[i].legendname);
    console.log(json[i].legenddescription);
    for(j = 0; j<json[i].rows.length; j++) {
        console.log("new row");
        console.log('\t' + json[i].rows[j].uid);
        console.log('\t' + json[i].rows[j].legendrowname);
        console.log('\t' + json[i].rows[j].fillcolor);
        console.log('\t' + json[i].rows[j].bordercolor);
        // and so on...
    }
}

编辑2

为什么不只是创建为javascript对象,如果只需要字符串化后需要json

这样的事情(可能不准确):

var objArr = new [];
$.each(dataL, function(i, item){
    var leg = new Object();
    leg.uid = item.uid;
    leg.legendname = item.legendname;
    leg.rows = [];
    $.each(dataR, function(j, item2){
        var rw = new Object();
        rw.uid = item2.uid;
        rw.legendrowname = item2.legendrowname
        // and so on
        leg.rows.push(rw);
    });
    objArr.push(leg);
});

alert(JSON.stringify(objArr));

答案 1 :(得分:0)

输出缺乏围绕它的支撑。添加它们,它应该工作。