0

I'm working to understand jQuery with a pretty good knowledge of PHP.

I have a string like so:

[donor="company1" web="http://company1.com" amount="5.50"];[donor="company2" web="http://company2.com" amount="40.00"]

I am trying to achieve the answered result from this question.

Unfortunately, I have no idea how to turn my string into something like what the asker of the other question has to begin with, i.e.:

var data = [{'donor':'company1', 'web':'http://company1.com', 'amount':5.50}, 
            {'donor':'company2', 'web':'http://company2.com', 'amount':40.00}]

Any help you can offer would be awesome. I assume it has something to do with map, associative array, object, but I am just not getting it.

Thanks in advance, I really appreciate it!

2
  • Your problem is not really related to that question. You have a string of non-formatted data, it's not an array or object or anything javascript will parse. If that really is a string then you'll have to parse it. Commented Apr 25, 2012 at 22:20
  • It's so similar to a JSON array of objects, that it can be easily converted in a real JSON which can be evaulated by JavaScript. You can check my answer. Commented Apr 25, 2012 at 23:19

1 Answer 1

0

You can use regex replacemnt to convert the original string to the required string:

var s='[donor="company1" web="http://company1.com" amount="5.50"];[donor="company2" web="http://company2.com" amount="40.00"]';
s='[' + s.replace(/\[/g,'{').replace(/\]/g,'}').replace(/=/g,':')
  .replace(/"\s/g,'", ').replace(/{(.+?):/g,'{"$1":')
  .replace(/;/g,',\r\n') + ']';

The result of this conversion is the string that you want to get, which is a JSON notation of an array of objects.

If you eval this JSON expression you get the array of objects.

If you add these lines to the end of the previous script, you'll see that you get an array of length 2, which contains the objects represented by the JSON:

var t = eval(s);
alert(t.length); // output 2, which is the array length
alert(t[0].donor); // outputs company1, which is the donor of the first object in the array

You can play around with this code in w3schools try it yourself editor.Copy and paste this code:

<html>
<head>
<script type="text/javascript">
var s='[donor="company1" web="http://company1.com" amount="5.50"];[donor="company2" web="http://company2.com" amount="40.00"]';
s='[' + s.replace(/\[/g,'{').replace(/\]/g,'}').replace(/=/g,':')
  .replace(/"\s/g,'", ').replace(/{(.+?):/g,'{"$1":')
  .replace(/;/g,',\r\n') + ']';
var t = eval(s);
alert("Array length: " + t.length);
alert("1st object donor: " + t[0].donor);
</script>
</head>
<body>


</body>
</html> 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank @jotabe, that makes sense. But I have another followup question. Once I get the array, I pass it to the for loop from the answer from this question link[link], as I stated my intent originally. After the for loop, I need to get it back to the original string format. I guess I'd have to stringify the array and then reverse the regex?
I think I can use JSON.stringify(t) to get it into a string. @jotabe Any help on the reverse regex? Thanks!
Yes, but I think I got it figured out with this var u = JSON.stringify(t); u = u.replace(/\[/g,'').replace(/\]/g,''); u = u.replace(/\{/g,'['); u = u.replace(/\}/g,']'); u = u.replace(/\],\[/g,'];[').replace(/:/g,'='); u = u.replace(/\["/g,'['); donors = u.replace(/"=/g,'=').replace(/,"/g,',').replace(/",/g,'" '); Not as elegant as your original regex, but I think it works out.

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.