tonoslfx
tonoslfx

Reputation: 3442

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:

hope my question clears enough :)

Upvotes: 0

Views: 4258

Answers (3)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

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" />

Upvotes: 3

Sushanth --
Sushanth --

Reputation: 55750

Firstly your the syntax looks a bit messed up , Surround your value using single Quotes

value='{"id":"1","price":"30.00","edit":0}, {"id":"2","price":"8.00","edit":0}'

Upvotes: 0

epascarello
epascarello

Reputation: 207511

That will not work since you have quote problem. Easiest solution is to surround it in single quotes.

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

Do not split it, use JSON.parse

Upvotes: 0

Related Questions