0

I have got the code:

$(document).ready(function() {

  var adress = [];
  var locations = ' [';
  $('.office p').each(function(el) {
    adress[el] = $(this).text();
    locations = locations + '{address:\''+adress[el]+'\', data: \'0\', options:{icon: "http://selectner.com/img/bullet.png"}},';
  });

  locations = locations + ']';
        console.log(locations);


     $('#tablink').click(function (e) {

     $('#map').gmap3({
      map:{
         options:{
           center:[51.4675954,0.048876],
           zoom: 2,
           scrollwheel: true,
          draggable: true, 
          }
      },
     marker:{
         values : locations,
         options:{
          draggable: true
        },
        events:{
                    }
    }
   });
  });
});

If I will copy console.log results and paste it into JSON (in gmap3 function - locations place) - all will work, but now it is not working.

I think, that I must to use JSON.parse(), but it write error to me:

SyntaxError: JSON.parse: expected property name or '}' at line 1 column 3 of the JSON data

How I can solve this problem?

9
  • Paste the log output here. Commented Mar 14, 2015 at 19:57
  • That's not valid JSON even. Commented Mar 14, 2015 at 19:57
  • 1
    Stop trying to cobble together JSON yourself – create proper data structures out of arrays/objects, and then have those encoded as JSON when needed using the browser’s native methods (or a framework/polyfill if necessary). Commented Mar 14, 2015 at 19:59
  • @LShetty: I rebuild with double squares, but it still don't work. Now code console.log is [{"address":'Toronto, SCARBOROUGH JUNCTIO', "data": '0', "options":{"icon": 'http://selectner.com/img/bullet.png'}},{"address":'Moscow, tverskaya street, 18', "data": '0', "options":{"icon": 'http://selectner.com/img/bullet.png'}},{"address":'Leipzig, SÜDVORSTADT', "data": '0', "options":{"icon": 'http://selectner.com/img/bullet.png'}},] Commented Mar 14, 2015 at 20:16
  • You still have invalid JSON over there. That's why it doesn't work. Maybe you can try simply using a JSON serializer instead of concatenating those strings. Commented Mar 14, 2015 at 20:18

2 Answers 2

1

Don't build JSON using string concatenation operations. Just don't. Never, ever.

You may try using a simple JSON serializer which is designed for this purpose so that you never get invalid JSON:

var addresses = [];
$('.office p').each(function() {
    addresses.push({
        address: $(this).text(),
        data: 0,
        options: {
            icon: 'http://selectner.com/img/bullet.png'
        }
    });
});

var locations = JSON.stringify(addresses);
console.log(locations);
// At this stage it is guaranteed that the addresses variable will
// contain valid JSON string
Sign up to request clarification or add additional context in comments.

5 Comments

I agree with you, but OP is using a plugin and this is how the documentation says how to do it :/
Oh that's really weird. Could you point to this documentation? Maybe the authors of it should be notified about the mistake they did. I mean if a documentation is showing an example where JSON is being built with string concatenation operations, then, well, OMG.
I am not sure where in this example a JSON is being built using string concatenations. They seem to be building some DOM elements using string concatenations (which is also bad) but not JSON.
The OP's problem is that he is building JSON using string concatenation operations. Chances of getting this right are close to 0.
Yep I see now, sorry about the confusion. Well also trying to build your own JSON, we all have been there :)
0

That's because your "string" isn't valid JSON. Read more about the structure of JSON here: http://json.org/

Also, you're already in JavaScript, why are you building JSON when you can already use objects?

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.