Search code examples
javascriptjqueryjsonstringify

JavaScript json split object and update


How do i split json object and update them according to the ID. I heard to use stringify! and how do i implement the function to update the object?

<input type="text" value="{"id":"1","price":"30.00","edit":0},
{"id":"2","price":"8.00","edit":0}" id="json" />

**code:**
var json = $('#json').val().split(',');

for (var i = 0; i < json.length; i++){
  alert(json);
}
//(seems its splitting every comma it finds).

Im trying to archive:

  • Split the object {"id":"1","price":"30.00","edit":0}, {"id":"2","price":"8.00","edit":0}
  • Update depends on selected ID.
  • and then save and it return same object format but with updated value.

hope my question clears enough :)


Solution

  • Instead of string split and parsing.. Try using $.parseJSON like below,

    $.parseJSON("[" + $('#json').val() + "]");
    

    DEMO: http://jsfiddle.net/QUTu9/

    Also fixed the quotes in html like below,

    <input type="text" value='{"id":"1","price":"30.00","edit":0},
    {"id":"2","price":"8.00","edit":0}' id="json" />