-1

我有一个 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 []

它被识别为一个对象(这是我下一步需要的)

现在,我正在尝试使用 legend2 动态创建它

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.parse, eval, 等等...但没有人工作。

¿ 有人可以帮我或给我小费吗?

编辑:传入的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
        }   
    }}
4

2 回答 2

0

输出周围缺少大括号。添加这些,它应该工作。

于 2013-10-29T15:45:38.123 回答
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": ""
        }
    ]
}

如果有效,请使用http://jsonlint.com/检查传入的 JSON 。您自己生成了 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

如果您在字符串化之后需要 json,为什么不直接创建为 javascript 对象

像这样的东西(也许不准确):

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));
于 2013-10-29T15:45:01.130 回答