2014-03-03 54 views
0

我需要發送json格式的同一組值。json發送相同的值集

即我有兩組鍵和值,鍵名是相同的,值是不同的,我需要像一個字符串數組。

鍵[10] = {}名稱值 [10] = {}值

我試圖像下面格式

{ ... 「retweeted_status」:{ ... 「ID_STR」: 「114345368862461952」, ... }, ... 「ID_STR」: 「114369052268437504」, ... } 但它需要很大的空間。

有人可以告訴如何以json格式在這個(數組)中表示使用standrad json來發送和正確解析。

回答

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>