0

I have an input on a form which stores values as an object.

jQuery('#inputId').val()

returns something like

'[{"Id":"123","Name":"A","PathOfTerm":"A","Children":[],"Level":0,"RawTerm":null},{"Id":"234","Name":"B","PathOfTerm":"B","Children":[],"Level":0,"RawTerm":null}]'

as one single string. Is there any way to either prevent this from automatically converting to a string (maybe not using .val?) or to convert this from a string to something I could work with?

George
6,7592 gold badges31 silver badges36 bronze badges
asked May 16, 2017 at 13:02
7
  • So you want to have it as a object ? Commented May 16, 2017 at 13:03
  • 4
    you could JSON.parse this Commented May 16, 2017 at 13:04
  • Looks like it's returning JSON. Lucky for you most browsers have the function JSON.parse to convert it to an object Commented May 16, 2017 at 13:04
  • Possible duplicate of Parse JSON in JavaScript? Commented May 16, 2017 at 13:06
  • Possible duplicate of Convert JSON string to array of JSON objects in Javascript Commented May 16, 2017 at 13:06

1 Answer 1

5

Here you go

var array = JSON.parse(jQuery('#inputId').val());
answered May 16, 2017 at 13:04
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.