0

I am planning to have coordinates sent to the google maps API but i am unable to delete the field name from my json object in order to send the parameters through.

Object {TempPoints: "{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192…{lat: 51.47840998047034,lng: -3.1926937697490536}"}

how do i remove 'TempPoints:' from the object

desired output

Object {"{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192…{lat: 51.47840998047034,lng: -3.1926937697490536}"}

essentially i am trying to recreate something like this

flightPlanCoordinates = [{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.47845554862494,lng: -3.1928923123350774},{lat: 51.47848027862647,lng: -3.1929894662780804}];

PHP Code as requested

$sql = $dbh->prepare("SELECT TempPoints FROM session WHERE CustomerID = 2 ORDER BY SessionID DESC LIMIT 1"); 
    $sql->execute(); 
    $row = $sql->fetch(PDO::FETCH_ASSOC);

    $para = implode(" ",$row);
    echo json_encode($row);

Answer

Object {TempPoints: "{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192…{lat: 51.47840998047034,lng: -3.1926937697490536}"}
  • What language do you expect to use for the answer? JS or PHP? – Victor Smirnov May 12 '16 at 08:10
  • either aslong as it gets the job done, currently i parse the object from my PHP file back to the javascript using ajax and convert the json string using 'flightPlanCoordinates = JSON.parse(data)'. and get the error 'SyntaxError: Unexpected token o in JSON at position 1' – Hash Velani May 12 '16 at 08:13
  • Please show your PHP code where you response to AJAX request and send the answer. – Victor Smirnov May 12 '16 at 08:14
  • this link may be help full to you http://stackoverflow.com/questions/15451290/remove-element-from-json-object – Gomzy May 12 '16 at 08:15
  • @VictorSmirnov just updated the question – Hash Velani May 12 '16 at 08:29

3 Answers3

1

Objects need to have key:value pairs so your desired output is not valid syntax.

It looks like you might want an array of objects:

var array = [
    {
        lat: 51.478,
        lng: -3.192
    },
    {
        lat: 51.478,
        lng: -3.192
    },
    {
        lat:51.47840998047034,
        lng: -3.1926937697490536
    }
];
Clijsters
  • 4,031
  • 1
  • 27
  • 37
smythluke
  • 191
  • 11
  • this makes sence , in my database i have my coordinates stored in this way "{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.47845554862494,lng: -3.1928923123350774},{lat: 51.47848027862647,lng: -3.1929894662780804}" how would I convert this to an array of objects? – Hash Velani May 12 '16 at 08:23
  • Yo can try with this: `JSON.parse("[" + "{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.47845554862494,lng: -3.1928923123350774},{lat: 51.47848027862647,lng: -3.1929894662780804}" + "]");` – dlopez May 12 '16 at 08:32
  • you have a string: `string = "{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.478,lng: -3.192},{lat: 51.47845554862494,lng: -3.1928923123350774},{lat: 51.47848027862647,lng: -3.1929894662780804}"` to get an array of objects from this particular string: `result=string.match(/[^,]+,[^,]+/g);` `objects = []` `for (var index in result){objects[index] = eval("("+result[index]+")");}` You should now have an array of objects called `objects` – smythluke May 12 '16 at 08:54
  • Note the regex above: `string.match(/[^,]+,[^,]+/g);` is for this string type only where we are splitting the string on every 2nd comma. This is because you have commas between the lat and lngs and also the objects them selves. – smythluke May 12 '16 at 08:55
  • thank you @smythluke what you suggested did the job. much appreciated – Hash Velani May 12 '16 at 15:40
0

Using JavaScript:

var obj = {TempPoints: "{'lat': 51.478,'lng': -3.192},{'lat': 51.478,'lng': -3.192},{'lat': 51.47840998047034,'lng': -3.1926937697490536}"};

var obj = "["+obj.TempPoints+"]";

var newObj = eval(obj);

console.log(newObj); //It's an array of Objects

JSFiddle

Jenson M John
  • 5,499
  • 5
  • 30
  • 46
0
echo json_encode($row['TempPoints']);

Is this what you want?