0

I have a online JSON file that looks something like this:

[
{
"j": 0,
"i": 0,
"DepartureTime": "\/Date(1331667480000+0100)\/",
"ArrivalTime": "\/Date(1331668860000+0100)\/",
"Remarks": [],
"TravelStages": [
  {
    "ID": 0,
    "DepartureStop": {
      "WalkingDistance": 0,
      "ArrivalTime": null,
      "AlightingAllowed": false,
      "DepartureTime": null,
      "BoardingAllowed": false,
      "RealTimeStop": true,
      "Rank": 0,
      "Lines": null,
      "StopPoints": [
        {
          "ID": 1,
          "Name": "1",
          "X": 608127,
          "Y": 6645778
        }
      ],
      "Zone": "1",
      "X": 608133,
      "Y": 6645768,
      "ID": 2300500,
      "Name": "Visperud (i Solheimvn)",
      "District": "Lørenskog",
      "Type": 0,
      "Stops": [],
      "ShortName": "VIS"
    }]

What I want is the grab out the DepartureTime and ArrivalTime, I've seen some examples on how to parse the flickr JSON. But I can't figure out how I can parse this. I also want to store the departureTime and arrivalTime in two separate variables since the content of this two is a time measured in milliseconds since 1970. Can somebody give me a hint on how a can do this, am totally new to Javascript/JSON

2
  • That isn't valid JSON according to JSONLint.com Commented Mar 14, 2012 at 14:57
  • it's a part of my JSON file, but its too long for posting her. Commented Mar 14, 2012 at 14:58

5 Answers 5

1

Do you have jQuery in your project? If so, you can easily parse the JSON string like this

var obj = $.parseJSON(theJsonText);
alert(obj.DepartureTime);

If not, I suggest including the JSON library (link) and using that.

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

6 Comments

But how can I specify the URL? just replacing the parameter? :)
what URL? you didn't mention you don't have the string. then you'll need AJAX to get it... please clarify your question: you don't want to parse JSON, you want to download and parse JSON.
He says he has an online JSON file, therefore needing to download it.
As my first sentence says: "I have a online JSON file that looks something like this" So I need to download it and parse it. But this file is constantly updated, so in this case I can't download it just one time, I guess.
yeah, but e.g. many PHP scripts directly dump the json into the generated javascript, so it wasn't quite clear. I'll write an example below...
|
1

You can try something like this, assuming that your json file is in jsonfile.json

$.getJSON('jsonfile.json', function(data){
    alert("Departure Time: "+ data.DepartureTime);
    alert("Arrival Time: "+ data.ArrivalTime);
});

http://api.jquery.com/jQuery.getJSON/

Comments

1
$.getJSON('http://your.domain.example/path/to/file.json', function(data) {
  departure_time=data.DepartureTime;
  arrival_time=data.ArrivalTime;
  do_something_with(departure_time,arrival_time);
});

then do_something_with(str,str) would be called with the strings "\/Date(1331667480000+0100)\/" and "\/Date(1331668860000+0100)\/" (in your example). you'll still have to convert the dates to numbers, e.g. by running:

parsed_date=new Date(parseInt(input_string.substr(7)));
//substr(7) cuts after "\/Date(", and parseInt ignores ")\/"
//but I don't know how it handles "+0100"

Comments

0

Thats an array containing objects, so you should be able to just set some vars equal to the properties of the first index. to use it like an object, it needs to be parsed.. so either eval(thatJson) or $.parseJSON(thatJson) and then iterate through it.

var responses = [
{
"j": 0,
"i": 0,
"DepartureTime": "\/Date(1331667480000+0100)\/",
"ArrivalTime": "\/Date(1331668860000+0100)\/",
"Remarks": [],
...
}];

var dep = responses[0].DepartureTime;
var arr = responses[0].ArrivalTime;

Comments

0

According to JSONLint.com, your string isn't valid JSON. That is, however, a different issue than what your question asks for.

Assuming a valid subset of your string

var a = '[{"j": 0,"i": 0,"DepartureTime": "/Date(1331667480000+0100)/",      "ArrivalTime": "/Date(1331668860000+0100)/","Remarks": []}]';

var obj = $.parseJSON(a);
console.log(obj[0].ArrivalTime);​

Comments

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.