SWFApp February 2016

JSON app not responding with web code

I have written out some JSON code, uploaded it to my FTP server, and used it in my JSON app. I have also written the codes for my app in Xcode. I use Swift. The problem is that when I run my app, nothing is returned in the UITableView. How do I make the text appear in my iOS Simulator?

UPDATE

I have followed Scriptable's guidance, and this is my new error in Xcode:

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

UPDATE ENDED

Here is my JSON web code (in a .json file):

         {"items":[{

            "title" : "Big Little Lies Book Review",
            "date" : "WEDNESDAY 3RD FEBRUARY, 2016",
            "content" : "Few spoilers ahead. I finished reading Big Little Lies by Liane Moriarty a few days ago, and I haven't stopped thinking about it since! The book is so gripping,and the characters (what I love most) are so different. You have Madeline Mackenzie, a bubbly, bright 40 year old; Celeste White, a beautiful, weak lady who is married to a rich businessman; and Jane Chapman, a quiet, 24-year old mum with a mysterious past. The novel is set in Pirriwee, Australia. These characters' children all join Pirriwee Public, beginning Kindergarden. A previous orientation day drama still lingers between the mothers, especially the victim's mum, Renata Klein, and the so-called bully's mum, Jane (at this point, we still are unaware of who the bully is.) The story leads up to the annual trivia night of the school, with the end of each chapter showing a small extract of other mothers recounting the night. A detective is questioning them regard to a murder. In these extracts, we see that there is a conflict between Madeline, Celeste, and Jane as one, and the other mothers (not as much the fathers.) Moriarty makes connections between each character, each conflict, and each        

Answers


Simon McLoughlin February 2016

ok, I think the problem is that you think the first block of code you posted on your question is JSON? Its not, its HTML and Javascript. Thats why your JSON parser is failing.

You have written a webpage, not a webservice, they are very different things. You have a few options:

  • Read up on webservices and create one (recommended)
  • Use a HTML parser, download the page and take out the bits you want
  • Load the HTML into a webview and use javascript to pull out the bits you want

The last two are only there for completeness sake, you should go for the first one.


Scriptable February 2016

Your current code is just a webpage, You could create a server side script to generate and return valid JSON for a particular URL or you could just create a .json file.

For example:

create a blank file called example.json and put the following in it

 {
"items": [{
    "title": "Allegiant stills released",
    "date": "FRIDAY 29TH JANUARY, 2016",
    "content": "Some more Allegiant stills have been released - as if we can't get enough! The two that we have obtained of Shailene's character, Tris, have been added to the Allegiant gallery in Photos."
}, {
    "title": "Allegiant stills released",
    "date": "FRIDAY 29TH JANUARY, 2016",
    "content": "Some more Allegiant stills have been released - as if we can't get enough! The two that we have obtained of Shailene's character, Tris, have been added to the Allegiant gallery in Photos."
}, {
    "title": "Allegiant stills released",
    "date": "FRIDAY 29TH JANUARY, 2016",
    "content": "Some more Allegiant stills have been released - as if we can't get enough! The two that we have obtained of Shailene's character, Tris, have been added to the Allegiant gallery in Photos."
}, {
    "title": "Allegiant stills released",
    "date": "FRIDAY 29TH JANUARY, 2016",
    "content": "Some more Allegiant stills have been released - as if we can't get enough! The two that we have obtained of Shailene's character, Tris, have been added to the Allegiant gallery in Photos."
}]

}

Then upload to your server and in your iOS application use the following URL:

http://annabellesykes.byethost11.com/example.json

It would be worthwhile you reading up on generating the JSON 'on demand' though, try something like http://www.tutorialspoint.com/json/json_php_example.htm. It's probably not the best tutorial out there but should give you a good starting point and show you wh

Post Status

Asked in February 2016
Viewed 1,543 times
Voted 8
Answered 2 times

Search




Leave an answer