用于发送相同值集的json
我需要发送json格式的同一组vaules i、 我有两组键和值,键名相同,值不同,我需要类似字符串数组的东西 键[10]={names} 值[10]={values} 我试过下面的格式 { ... “转发状态”:{ ... “id_街”:“114345368862461952”, ... }, ... “id_街”:“114369052268437504”, ... } 但这需要很大的空间用于发送相同值集的json,json,jsonp,Json,Jsonp,我需要发送json格式的同一组vaules i、 我有两组键和值,键名相同,值不同,我需要类似字符串数组的东西 键[10]={names} 值[10]={values} 我试过下面的格式 { ... “转发状态”:{ ... “id_街”:“114345368862461952”, ... }, ... “id_街”:“114369052268437504”, ... } 但这需要很大的空间 有人能告诉我如何用json格式在这个(数组)中表示,以便使用standrad json正确发送和解析。这
有人能告诉我如何用json格式在这个(数组)中表示,以便使用standrad json正确发送和解析。这可能就是您正在寻找的,可以在这里找到:
var json=[{“id”:“5001”,“type”:“None”},
{“id”:“5002”,“type”:“glassed”},
{“id”:“5005”,“type”:“Sugar”},
{“id”:“5003”,“type”:“Chocolate”},
{“id”:“5004”,“type”:“Maple”},
{“id”:“5009”,“type”:“Juice”}];
/**
*函数通过特定字段值搜索数组,
*并用提供的参数替换发生的事件。
*
*@param string要比较的对象字段的字段名
*@param字符串oldvalue要与之比较的值
*@param string newvalue将mathes替换为
*/
函数replaceByValue(字段、旧值、新值){
for(var k=0;k
<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>