i have this code :
$wage = array();
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
if ($userid == 0 ) {
$age= "";
} else {
$age = implode(',', $wage).",";
}
echo json_encode("var2"=>$myvar ,"var3"=>$age)); // i didnt write var2 because is same as var3
the above will outputs something like [4,2.3][5,6],[1.7,5],
and in javascript im receiving this value outputed above via ajax which has name var3
.
dataType: 'json' ,
success: function(response){
var age = response['var3'] ;
.....
so the question is how can i parseFloat
this variable age
?
EDIT . if i alert the json like that
alert (response);
i get [object Object]
// make attention to small o and big O
EDIT2 . used consol log
and i get this
{"var2":"[2,5],[3.5,5],[6.5,6.5],[8,7],","var3":"[2,46],[3.5,61],[6.5,70],[8,71],","var4":"[2,32],[3.5,41],[6.5,42],[8,43],","var5":"[46,5],[61,5],[70,6.5],[71,7],"}
this returned values i want to parseFloat
them as you see they are like strings between two quotes
i have this code :
$wage = array();
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
if ($userid == 0 ) {
$age= "";
} else {
$age = implode(',', $wage).",";
}
echo json_encode("var2"=>$myvar ,"var3"=>$age)); // i didnt write var2 because is same as var3
the above will outputs something like [4,2.3][5,6],[1.7,5],
and in javascript im receiving this value outputed above via ajax which has name var3
.
dataType: 'json' ,
success: function(response){
var age = response['var3'] ;
.....
so the question is how can i parseFloat
this variable age
?
EDIT . if i alert the json like that
alert (response);
i get [object Object]
// make attention to small o and big O
EDIT2 . used consol log
and i get this
{"var2":"[2,5],[3.5,5],[6.5,6.5],[8,7],","var3":"[2,46],[3.5,61],[6.5,70],[8,71],","var4":"[2,32],[3.5,41],[6.5,42],[8,43],","var5":"[46,5],[61,5],[70,6.5],[71,7],"}
this returned values i want to parseFloat
them as you see they are like strings between two quotes
-
1
You should use JSON (
json_encode()
) rather manually build JS code. – Crozin Commented Dec 6, 2012 at 21:10 - im using json encode i will edit my post – echo_Me Commented Dec 6, 2012 at 21:11
- Please post the exact JSON output you're getting. – bfavaretto Commented Dec 6, 2012 at 21:18
-
You should be using
json_encode()
for the whole thing, not just the last step. – user149341 Commented Dec 6, 2012 at 21:18 -
alert wont be as helpful with objects as
console.log
will. Trying logging it to the console and hitting F12 (in FF and chrome) to see what log message you get – lbstr Commented Dec 6, 2012 at 21:26
2 Answers
Reset to default 3This is incorrect:
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
You're building a string, which coincidentally happens to LOOK like a javascript array definition, but it's still just a string. When this gets json_encoded, it'll e out as
"[...,...]"
^-- ^-- string
You need to build NATIVE data structures at all stages while in PHP, e.g.
$wage[] = array(floatval($row3['age']), floatval($row3['point']));
and not this mish-mash of native AND "json-like" strings. json_encode() converts NATIVE datatypes of the equivalent Javascript types. Since your own floatval business is producing a string, you'll get a json-encoded string, not an array.
You are getting this all wrong. You do not need to do this;
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
Perhaps what you want is;
foreach ($result3 as $i => $row3) {
$newRow = $row3;
$newRow['age'] = intval($row3['age']);
$newRow['point'] = floatval($row3['point']);
$result3[$i] = $newRow;
}
And then do this;
// Create JSON data, assuming that $result3 is an array
$jsonData = json_encode($result3);
// This will give you a JSON string, output this and finish to ensure it IS the only thing output
echo $jsonData;
die;
Now in your javascript, open the development console in what ever browser your using and use the following code in javascript
console.log(response)
This will output the whole response
variable to the console and enable you to debug how to get specific data out of the response
var.
Hope that helps.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745594954a4635054.html
评论列表(0条)