2

I have this wrong Json string:

({loc: '(49.150892549035454, 9.22825813293457)' , shopName: 'Otest', nameID: '1', apID: 'Loa', gender: 'male'}); ({loc: '(12.150892549035454, 12.22825813293457)' , shopName: 'OtestRe', nameID: '1', apID: 'Loaa', gender: 'female'});

So now I tried to correct the string with:

        var res = this.markerlist.replace(/\)|\(/g, '');
        var res2 = res.replace(/;/g, ',');
        var jsonList = JSON.stringify('[' + res2 + ']';

[{loc: '(49.150892549035454, 9.22825813293457)' , shopName: 'Otest', nameID: '1', apID: 'Loa', gender: 'male'}, ({loc: '(12.150892549035454, 12.22825813293457)' , shopName: 'OtestRe', nameID: '1', apID: 'Loaa', gender: 'female'}]

Then I try to parse it:

   var jsonRs = JSON.parse(jsonList);


     for (var rowData in jsonRs) // for acts as a foreach
    {
        console.log(jsonRs[rowData]);

    }

And I get every character as output like:

4
9
.
1

And so on.

7
  • 1
    jsonlint.com Commented Dec 19, 2015 at 17:03
  • You should use JSON.parse not JSON.stringify. Commented Dec 19, 2015 at 17:04
  • You should not iterate over an array with for... in but with for (var i = 0; i < myArray.length; i++) Commented Dec 19, 2015 at 17:08
  • 1
    It seems like you are using a JSONP service and you forgot the callback URL parameter. Like this: http://example/com/get.json?callback=test Commented Dec 19, 2015 at 17:46
  • 1
    @user3387996 You want me to add it as an answer? Commented Dec 19, 2015 at 18:05

3 Answers 3

4

Although as everyone suggested, knowing the format is important to solve your problem. So your approach to replace & parse is wrong & error prone.

Your Json string is actually javascript... So even an eval could work.

EDIT : As pointed out by Biwise Creative, it would need bit of treatment :

this.markerlist.split(";").slice(0,-1).map(function(t) {return eval(t);})
Sign up to request clarification or add additional context in comments.

1 Comment

This won't work well with his string as-is since there are multiple objects. He'll end up with a single object with the values of the last data set. You'd still need to split and format the string data sets.
1

You can parse your input like this:

var input = "({loc: '(49.150892549035454, 9.22825813293457)' , shopName: 'Otest', nameID: '1', apID: 'Loa', gender: 'male'}); ({loc: '(12.150892549035454, 12.22825813293457)' , shopName: 'OtestRe', nameID: '1', apID: 'Loaa', gender: 'female'});";

var result = input
             .split(";") // spliting ';' for each object
             .slice(0, -1) // removing last portion after ';'
             .map(function(item) {
                 var cleaned = item
                     .trim() // removing white spaces from the ends
                     .slice(1, -1) // getting rid of ()
                     .replace(/'/g, '"') // replacing ' to "
                     .replace(/(\w+):/g, '"$1":'); // surrounding word before ':' with double quotes
                 return JSON.parse(cleaned);
             });

console.log(result);

Comments

0

Here another version with loc property as Array so you could use lattitude ang longitude

 var markerlist = "({loc: '(49.150892549035454, 9.22825813293457)' , shopName: 'Otest', nameID: '1', apID: 'Loa', gender: 'male'}); ({loc: '(12.150892549035454, 12.22825813293457)' , shopName: 'OtestRe', nameID: '1', apID: 'Loaa', gender: 'female'});";

    var arStr = markerlist.split(";");
    var jsons = [];

    for(var i=0;i < arStr.length;i++){
            var o = eval(arStr[i]);
                    if(o){
                            o.loc = o.loc
                                    .replace(/\(|\)/g, "")
                                    .split(',');
                            jsons.push(o);
                    }
    }
    console.log(jsons);

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.