javascript - Assigning identifiername to JSON object in PHP - Stack Overflow

I'm grabbing data from a mysql database and encoding a JSON object with PHP to use in JS. On the

I'm grabbing data from a mysql database and encoding a JSON object with PHP to use in JS. On the PHP end, I did this

while($row = mysql_fetch_array($result))

{
     $jmarkers = array(
        'id'=> $row['id'],
                'lat' => $row['lat'],
                'lng' => $row['lng'],
         etc...            
    );
    array_push($json, $jmarkers);
}

$jsonstring = json_encode($json);
echo $jsonstring;

I can access the data in JS using jQuery, and I made an array to save the JSON data:

$.getJSON("getjson.php", function(data)

{
     myMarkers = data;
     console.log(myMarkers);
});

I'd planned to access the data in the myMarkers array inside loop, with a statement like this:

var tempLat = myMarkers.jmarkers[i].lat;

The problem is my JSON objects aren't called jmarkers or anything else, they have this generic name "Object" when I print them to the console:

Object { id="2", lat="40.6512", lng="-73.9691", more...},

So I'm not sure how to point to them in my JS array. I looked the PHP JSON encode function and I can't see where to set or change the object name. Any suggestions? Thank you!

I'm grabbing data from a mysql database and encoding a JSON object with PHP to use in JS. On the PHP end, I did this

while($row = mysql_fetch_array($result))

{
     $jmarkers = array(
        'id'=> $row['id'],
                'lat' => $row['lat'],
                'lng' => $row['lng'],
         etc...            
    );
    array_push($json, $jmarkers);
}

$jsonstring = json_encode($json);
echo $jsonstring;

I can access the data in JS using jQuery, and I made an array to save the JSON data:

$.getJSON("getjson.php", function(data)

{
     myMarkers = data;
     console.log(myMarkers);
});

I'd planned to access the data in the myMarkers array inside loop, with a statement like this:

var tempLat = myMarkers.jmarkers[i].lat;

The problem is my JSON objects aren't called jmarkers or anything else, they have this generic name "Object" when I print them to the console:

Object { id="2", lat="40.6512", lng="-73.9691", more...},

So I'm not sure how to point to them in my JS array. I looked the PHP JSON encode function and I can't see where to set or change the object name. Any suggestions? Thank you!

Share Improve this question edited Aug 4, 2012 at 1:54 Daniel Li 15.4k6 gold badges44 silver badges60 bronze badges asked Jul 16, 2012 at 16:06 zannahzannah 416 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

That's to be expected. JSON is essentially the right-hand-side of an assignment operation:

var x = {'foo':'bar'};
        ^^^^^^^^^^^^^---- JSON

The x part is not included, since that's simply the name of the object. If you want your jmarkers text included, it'll have to be part of the data structure you're going to encode:

$arr = array(
   'jmarkers' => array(...your data here...);
);

But all this does is add another layer to your data structure for no useful reason.

$jmarkers is simply the identifier on the PHP side for the JSON object. When it gets passed, it converts the array value into a JSON-encoded string and therefore loses the identifier as a result.

In your PHP code at the moment, array_push($json, $jmarkers) is appending an array to your current $json array. You are therefore instancing a two-dimensional array, which will not be retrievable by the jmarkers identifier in your Javascript code. Simply retrieve the data using myMarkers[i] instead.

You... don't. The whole thing is an object. You only need to refer to the elements within.

alert(myMarkers.id);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742398399a4436412.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信