如何更改json键:值

时间:2010-12-29 10:18:48

标签: json replace set

//my json data    
var jsndata = "{ "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" },
            { "id": "5009", "type": "Juice" }"

如何改变“类型”:“巧克力”值=> “类型”:“只有水”
或..“id”:“5005”,=> “id”:“1234”,

我的列表很长..我需要获取任何值或设置任何值?

注意:我的列表是动态的。总是按ID或类型排序。

jsndata.id ['5003'] ='1234'改变..? var getval = jsndata.id ['5005']。type get val ..(value Sugar)?

:d

4 个答案:

答案 0 :(得分:20)

<script>
var json = [{ "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" },
            { "id": "5009", "type": "Juice" }];
/**
 * The function searches over the array by certain field value,
 * and replaces occurences with the parameter provided.
 *
 * @param string field Name of the object field to compare
 * @param string oldvalue Value to compare against
 * @param string newvalue Value to replace mathes with
 */
function replaceByValue( field, oldvalue, newvalue ) {
    for( var k = 0; k < json.length; ++k ) {
        if( oldvalue == json[k][field] ) {
            json[k][field] = newvalue ;
        }
    }
    return json;
}

/**
 * Let's test
 */
console.log(json);

replaceByValue('id','5001','5010')
console.log(json);

replaceByValue('type','Chocolate','only water')
console.log(json);
</script>

答案 1 :(得分:2)

看看Pinch。以下是如何在您的案例中使用 Pinch 的简短示例。

var data = [
  {
    id: 5001,
    type: 'None'
  },
  {
    id: 5002,
    type: 'Glazed'
  },
  {
    id: 5005,
    type: 'Sugar'
  },
  {
    id: 5003,
    type: 'Chocolate'
  },
  {
    id: 5004,
    type: 'Maple'
  },
  {
    id: 5009,
    type: 'Juice'
  }
];

pinch(data, '/id/', function(path, key, value) {
  return (value === 5001) ? 5010 : value;
});

答案 2 :(得分:1)

试试这个。简化。

var json = [{ "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" },
                { "id": "5009", "type": "Juice" }];           

        var JsonObject= JSON.parse(json);
            $.each(JsonObject, function(key,value) {
                if( JsonObject[key].id=='5005' ){
                    JsonObject[key].id='1234';
                }             

                if( JsonObject[key].type=='Chocolate' ){
                    JsonObject[key].type='Only water';
                }

            });

答案 3 :(得分:1)

修改了上面的函数,以便能够更改键的所有值,并将其递增1。 你可以传入jsonObj

function replaceByValue( jsonObj, field, oldvalue, newvalue ) {
    for( var k = 0; k < jsonObj.length; ++k ) {
             jsonObj[k][field] = (newvalue *1)+k;

    }
    return jsonObj;
}

//示例

var json = [{ "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" },
                { "id": "5009", "type": "Juice" }]; 


json; 

replaceByValue( json, "id", "na", 123 );

json;