5

I have a PHP page which echos some JSON like so:

echo json_encode("Hello");

How can I access this in my swift app? I used a method to get data from the website before, when I println the data returned, it just prints all of the HTML code for the website (including tags).

    let url = NSURL(string: myLinkUrl)
    let request = NSURLRequest(URL: url!)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
        println(NSString(data: data, encoding: NSASCIIStringEncoding)) //Prints HTML code
    }

I would like to know how to only print the data that the page echos (in this case 'Hello')

Reece Kenney
  • 2,734
  • 3
  • 24
  • 57
  • [Refer to this question for accessing JSON in iOS](http://stackoverflow.com/questions/31234531/php-ios-what-is-best-way-to-encode-json-for-web-service/31284984#31284984) – mccbala Jul 08 '15 at 06:47
  • This answer will help you http://stackoverflow.com/questions/20399087/json-parsing-using-nsjsonserialization-in-ios – bicho Jul 29 '15 at 15:46

1 Answers1

0

Please make sure that you are printing just the json data in your php page. There should not be any html tags. The json should be in key-value pairs so that you can access it easily. A sample php file to generate valid json is given below.

<?php
 $response['data']="Hello";
 echo json_encode("Hello");
?>