如何更改json键:值

时间:2021-03-13 18:55:50
//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" }"

how to cahange "type": "Chocolate" value => "type": "only water"
or.. "id": "5005", => "id": "1234",

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

my list is very long.. im need get any value or set any a value ?

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

note : my list is dynamic. always sorting order by id or type..

注意:我的清单是动态的。总是按ID或类型排序顺序..

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

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

:D

4 个解决方案

#1


19  

<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>

#2


2  

Take a look at Pinch. Here is a brief example how Pinch could be used in your case.

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

#3


1  

try this. simplified.

尝试这个。简化。

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';
                }

            });

#4


1  

modified the function from above to be able to change all values of a key,and increment it by 1. And you can pass in the jsonObj

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

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

    }
    return jsonObj;
}

//example

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;

#1


19  

<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>

#2


2  

Take a look at Pinch. Here is a brief example how Pinch could be used in your case.

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

#3


1  

try this. simplified.

尝试这个。简化。

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';
                }

            });

#4


1  

modified the function from above to be able to change all values of a key,and increment it by 1. And you can pass in the jsonObj

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

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

    }
    return jsonObj;
}

//example

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;