如何更改json键:值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4553235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:42:10  来源:igfitidea点击:

how to change json key:value

jsonreplaceset

提问by NovaYear

//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 would i change "type": "Chocolate"=> "type": "only water"
or.. "id": "5005"=> "id": "1234"

我将如何更改"type": "Chocolate"=>"type": "only water"
或.. "id": "5005"=>"id": "1234"

My list is very long.. I need get or set any value ?

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

Note: My list is dynamic and always sorting order by id or type..

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

Will jsndata.id['5003']='1234'change it?

jsndata.id['5003']='1234'改变吗?

var getval = jsndata.id['5005'].typeget val..(value Sugar) ?

var getval = jsndata.id['5005'].type获得价值..(价值糖)?

回答by St.Woland

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

回答by Baggz

Take a look at Pinch, a (multi) data replacement tool for JavaScript objects/JSON. Here is a brief example how pinch.jscould be used in your case:

看看Pinch,一个用于 JavaScript 对象/JSON 的(多)数据替换工具。下面是一个简短的示例,说明如何在您的情况下使用pinch.js

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

回答by zero8

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

            });

回答by HattrickNZ

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;