how to change json key:value
//my json data
var jsndata = "{ "id": "500开发者_如何学Python1", "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"
"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..
Will jsndata.id['5003']='1234'
change it?
var getval = jsndata.id['5005'].type
get val..(value Sugar) ?
<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>
Take a look at Pinch, a (multi) data replacement tool for JavaScript objects/JSON. Here is a brief example how pinch.js could be used in your case:
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;
});
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';
}
});
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
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;
If you have multiple key-value pairs,you may use like this:
var json = [{
"AppSettings": {
"OBiAccessDomain": "ABCD",
"WebserviceUrlTokens": {
"$OBiWebService$": "http://",
}
}
}];
function replaceSingleField( field, oldvalue, newvalue ) {
if( oldvalue == json[0]['AppSettings'][field] ) {
json[0]['AppSettings'][field] = newvalue ;
}
return json;
}
function replaceTwoField( field1, field2, oldvalue, newvalue ) {
if( oldvalue == json[0]['AppSettings'][field1][field2] ) {
json[0]['AppSettings'][field1][field2] = newvalue ;
}
return json;
}
replaceSingleField('OBiAccessDomain','ABCD','XYZ');
console.log(json[0]);
replaceTwoField('WebserviceUrlTokens','$OBiWebService$','http://','https://');
console.log(json[0]);
精彩评论