0

I have JSON object data in jsp file and i have passed this data to javascript function through the hyperlink onclick method. i am writing the alert box in javascript function so it is displayed well but problem is i want to extract the urls in the JSON object and convert into an array..

the actual data in javascript function is below like that...

[Image{url='/images/HH/Images/US/NYC/NYC-MEL-1.jpg', description='Exterior', alt='null', sortOrder=1, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-9.jpg', description='Exterior', alt='null', sortOrder=1, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-11.jpg', description='Lobby', alt='null', sortOrder=4, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-5.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-6.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-7.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-8.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-10.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-3.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-2.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}, Image{url='/images/HH/Images/US/NYC/NYC-MEL-4.jpg', description='Guest Room', alt='null', sortOrder=34, externalUrl='null'}]

the above text contains the List of Image Objects contains urls and alt and description and etc...

i need solution in javascript to iterate the urls in java script and convert into an array..

please can u suggest the answer for me....

Thanks Ram Prasad G 08527960397

3
  • 1
    That's no JSON, sir. Looks like an Eclipse or Netbeans generated toString() method output. Commented Dec 13, 2012 at 7:02
  • If all are image objects, then why cant you fetch the required value, using "url" as the key while iterating the list? Commented Dec 13, 2012 at 7:06
  • Looks like you would need to turn all the equals to colons and add a colon between each Image and {, if you really, really wanted to turn it into an object in javascript only (imagining a regex replace). But then god help you if you ended up with an equals sign in one of your urls or descriptions or something. Commented Dec 13, 2012 at 7:13

2 Answers 2

1

That's not a valid JSON string. You will need to roll out your own parsing method to parse content off it correctly.

You should however be able to convert it to valid JSON by doing the following prior to calling a JSON.parse(..) on the resultant string.

  1. Replace "Image{" with "{"
  2. Replace all "=" characters with ":"
Sign up to request clarification or add additional context in comments.

1 Comment

Or he could do { Image : {.
1

I would use JQuery for this purpose. You can go for: http://api.jquery.com/jQuery.parseJSON/ and while iterating the loop of Images you can do whatever you like with them (including creating an array of them)

EDIT

Moreover, as the most people suggested, you're not providing JSON format in your quesion. As I asume, you have data generated by jsp (java), you could go first for POJO to JSON on the sever side, that would be on the client side read/parsed by javascript.

For the server side to generate JSON for you, I'd use: Jackson. See example on: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

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.