我需要以json格式发送相同的一组vaules。
即我有两组键和值,键名相同且值不同,我需要类似字符串数组。
key [10] = {names} 值[10] = {values}
我尝试过以下格式
{ ... “retweeted_status”:{ ... “id_str”:“114345368862461952”, ... }, ... “id_str”:“114369052268437504”, ... } 但它需要很大的空间。
有人可以告诉我如何用json格式表示这个(数组),使用standrad json正确发送和解析。
答案 0 :(得分:1)
这可能就是您要找的内容,请点击此处:how to change json key:value
<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>