1

Hey i sent a JSON encoded array to my JS page with two links.

{"download":"http://JapSeyz.dk/Test/Medarbejder/fileDownload.php?filename=test___1847598251_26-04-2013.png","view":"http://JapSeyz.dk/Test/Medarbejder/uploads/test___1847598251_26-04-2013.png"}

that is the output

it is in the array called status and if i try status[download] it is undefined, and status[0,1,2] and so on, just gives me a single character.... How do i select the whole link from download at once?

Thanks :)

Heres all the code.. Sorry (Some danish text, but i just want to display a Force DL link and a view link)

var links = JSON.parse(status);
    var download = status.download;
    var view = status.view;
    var status = "Brug dette link hvis folk kun skal se dette <br /><div class='link-copy'>http://JapSeyz.dk/Test/Medarbejder/"+view+"</div><br /><br />Hvis du vil have folk til at downloade den skal du bruge dette link <br /> <div class='link-copy'>"+download;
   //set the status message to that returned by the server
   document.getElementById('status').innerHTML=status;

And the PHP:

$test = json_encode(array("download" => $link, "view" => "http://JapSeyz.dk/Test/Medarbejder/".$move));

echo returnStatus($test);

Jesper Jacobsen
  • 151
  • 2
  • 13
  • 2
    Parse it using `obj = JSON.parse(status);`. – Rikesh Apr 26 '13 at 10:58
  • Check [json_decode](http://php.net/manual/en/function.json-decode.php) and [json_encode](http://php.net/manual/en/function.json-encode.php) PHP function to convert json to php array/object and php to json.. – Svetoslav Apr 26 '13 at 10:58
  • Please post also relevant Javascript code how to read the json object. Our magic globes do not wort today. – Reporter Apr 26 '13 at 11:00
  • Well i send it from at PHP page, but i am retrieving it again in a script of JS, not jQuery, just standard JS – Jesper Jacobsen Apr 26 '13 at 11:02
  • You are passing JSON response as array so you can't access it using status[download]. – Kailash Yadav Apr 26 '13 at 11:07

4 Answers4

1

Parse the json and display it like this edit.

var obj = jQuery.parseJSON('{"download":"http://JapSeyz.dk/Test/Medarbejder/fileDownload.php?filename=test___1847598251_26-04-2013.png","view":"http://JapSeyz.dk/Test/Medarbejder/uploads/test___1847598251_26-04-2013.png"}');
alert( obj.download);

Javascript

var obj = JSON.parse('{"download":"http://JapSeyz.dk/Test/Medarbejder/fileDownload.php?filename=test___1847598251_26-04-2013.png","view":"http://JapSeyz.dk/Test/Medarbejder/uploads/test___1847598251_26-04-2013.png"}');
alert( obj .download);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1
var jsonstring = $.parseJSON('{"download":"http://JapSeyz.dk/Test/Medarbejder/fileDownload.php?filename=test___1847598251_26-04-2013.png","view":"http://JapSeyz.dk/Test/Medarbejder/uploads/test___1847598251_26-04-2013.png"}');

for (var k in jsonstring) {
  alert("key is "+k);
   alert("value at key "+k+ " is "+ jsonstring[k]);
}
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0
var jsonobj = JSON.parse('{"download":"http://JapSeyz.dk/Test/Medarbejder/fileDownload.php?filename=test___1847598251_26-04-2013.png","view":"http://JapSeyz.dk/Test/Medarbejder/uploads/test___1847598251_26-04-2013.png"}');
alert( jsonobj .download);
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0
status1 = [
          {"download":"http://JapSeyz.dk/Test/Medarbejder/fileDownload.php?filename=test___1847598251_26-04-2013.png","view":"http://JapSeyz.dk/Test/Medarbejder/uploads/test___1847598251_26-04-2013.png"}
         ];

alert(status1[0].download);

Status is a array of objects so you should use status[index].yourPropName

SEE FIDDLE

Prasath K
  • 4,950
  • 7
  • 23
  • 35