0

Firstly, I am extremely new to JSON. I have been reading as much as I can on it. While I get the concept, implementing it is a different deal altogether for me.

So, I have an app which reads and displays data from JSON web pages. For instance, I can extract the time that is being shown in this website: http://date.jsontest.com/

Using the HTML from this website, I added the JSON Object to my HTML page in the following manner:

<html>
<body>
<pre>
{
   "score": "30-20"
}
</pre>
</body>
</html>

However, the app now throws a JSON exception everytime I try to retreive the score.

My question is, 'Is adding a JSON Object to the pre tag in an HTML page the correct way of creating a JSON Object on a web page?'

If not, what is the correct way to do it?

EDIT: This is is the code I am using in java to retrieve the JSON data:

    StringBuilder url = new StringBuilder(URL);
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if(status==200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        //JSONArray timeline = new JSONArray(0);
        JSONObject last = new JSONObject(data);
        return last;
    }
    else{
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        return null;
    }

The try statement:

try {
            json = lastTweet();
            return json.getString("time");
            //return "Oh Well";
        } 

Thanks.

17
  • 2
    A JSON file only has the JSON markup. No need to add HTML tags. How are you trying to retrieve the score? Commented Apr 8, 2014 at 18:32
  • 2
    Along with having only JSON markup, it's proper for the server to send the Content-type: application/json header to signify the type. Commented Apr 8, 2014 at 18:35
  • You need a javascript object withing the window scope or just want to display it in the page as text? Commented Apr 8, 2014 at 18:35
  • 2
    @Sid, press F5 while network tab is open Commented Apr 8, 2014 at 19:02
  • 1
    @vp_arth, OP is not using PHP. In Java, before you send the response, you can use response.setContentType("application/json");. Commented Apr 8, 2014 at 19:18

2 Answers 2

1

Your application should send the Content-type: application/json header and should only output the string representation of the data itself, so that your entire page is just:

{
   "score": "30-20"
}

and nothing more. The example that you gave follows the same procedure if you check the response headers and view the source code of that page.

The reason your parser is failing is because the page starts with <, when the first non-whitespace character should be {.

Sign up to request clarification or add additional context in comments.

5 Comments

If you're using PHP, and you have a PHP Associative Array $data to send, it's as simple as header('Content-type: application/json'); echo json_encode( $data );.
If it's a standalone file, my comment above shows the way to do it in Apache.
Here's what I aim to do though. I will be posting score updates and my application will pull data from a website. I will have my website displaying the scores. So how, if it is possible, can I create a web page with these JSON score Objects? Or is Apache the only way to do it? Thanks
You haven't specified how your application is pulling the data, a language would be a good start. Is your application saving them as an individual file periodically, or is it a server sided script which makes the call whenever a user requests for it?
Ah sorry about that. I'm using java to pull the data. I have added the code to the question. The client will keep trying to get the JSON data that is present in the said URL. So I will add a score update to my web page and the app will store all the updates in an Array.
1

Use something like this:

response.setContentType("application/json");
PrintWriter out = response.getWriter();
String json = "{\"data\": \"test\"}";
out.print(json);
out.flush();

on your dataserver

21 Comments

Sorry this could be an absolutely stupid question but where do I add this snippet?
@Sid, you write: I added the JSON Object to my HTML page in the following manner, where you print your html page?
I am using google app engine to deploy my HTML page to the web. Of course, that is causing problems. So, like you guys pointed out, I stopped trying to deploy an HTML file and deployed a JSON file instead. The problem now is that whenever I try to go to the website, it just ends up downloading a random file to my machine.
then my answer is not relevant
is gae a python? or this is SAS?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.