Search code examples
jsonjsonp

json for sending same set of values


I need to send the same set of vaules in json format .

i.e I have two set of key and values , key name is same and values are diffrent , I need something like an array of strings .

key[10] = {names} values[10] ={values}

I tried like below format

{ ... "retweeted_status" : { ... "id_str" : "114345368862461952", ... }, ... "id_str" : "114369052268437504", ... } but it requires lot of space.

can somebody tell how to represent in this (arrays) in json format to send and parse properly using standrad json .


Solution

  • This could be what you are looking for, Found here: 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>