0

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 :)

3 Answers 3

3

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" />
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.