2

I am trying to decode a JSON file in PHP (which I do all the time). But when I try and use this file: https://www.hyzyne.co.nz/updatewlg/nzta.json . It decodes it but then only shows the last part of the JSON in the array.

PHP:

$xml = file_get_contents('https://www.hyzyne.co.nz/updatewlg/nzta.json');

$data = json_decode($xml, true);

print_r($data);

If you view the file you will see it is quite long.

When I print the above all I get is:

Array ( [data] => Array ( [0] => Array ( [roadevent] => Array ( [alternativeRoute] => Follow Detours [directLineDistance1] => 1.55 km southeast of Taradale [directLineDistance2] => 1.62 km southwest of Jervoistown [directLineDistance3] => 1.66 km east of Waiohiki [endDate] => 2013-12-03T18:25:18.677+13:00 [eventComments] => Now Clear [eventDescription] => Crash [eventId] => 84295 [eventIsland] => North Island [eventType] => Road Hazard [expectedResolution] => Until further notice [impact] => Caution [locationArea] => SH 50 Taradale [locations] => Array ( [location] => 050-0005/04.88 Taradale ) [planned] => false [startDate] => 2013-12-03T17:17:00.000+13:00 [status] => Resolved [wktGeometry] => SRID=27200;POINT (2841479.57385071 6176775.805777006) [eventCreated] => 2013-12-03T17:20:18.450+13:00 [eventModified] => 2013-12-03T18:25:18.380+13:00 [informationSource] => Police [supplier] => Official [eventRegions] => Array ( [eventRegion] => Taranaki, Manawatu-Wanganui, Hawke's Bay & Gisborne Region ) ) ) ) )

Which is not all of the JSON, I did make the JSON file myself. But I am unable to recognise any problems in the JSON.

Even when putting my JSON through http://jsonlint.com/ I only get the last segment. Does anyone know what I am doing wrong?

Thanks

1

2 Answers 2

2

You have an issue with your json. You are repeating the property "roadevent". I'm assuming they all shouldn't be contained in the same object { inside the array [.

{
"data": [{
    "roadevent": {
        "alternativeRoute": "Local Roads",
        "directLineDistance1": "1.43 km west of Heathcote Valley",
        "directLineDistance2": "1.45 km southwest of Ferrymead",
        "directLineDistance3": "1.68 km southwest of Mount Pleasant",
        "eventComments": "Bridges Not To Be Crossed By Any Overweight Loads Except For Iso Containers Being Moved On Existing Overweight Permits.   Any Other Overweight Loads (including Those Travelling On Area Permits) Will Be Considered On A Case By Case Basis.",
        "eventDescription": "Other",
        "eventId": "47078",
        "eventIsland": "South Island",
        "eventType": "Road Hazard",
        "expectedResolution": "Until further notice",
        "impact": "Vehicle Restrictions",
        "locationArea": "SH 74 Christchurch - Tunnel Rd, Horotane Valley Overpasses No 1 And 2 (bsn 217 & 218)",
        "locations": {
            "location": "074-0019/02.60-D   Heathcote Valley"
        },
        "planned": "false",
        "restrictions": "Road constricted for oversize and non-standard vehicles",
        "startDate": "2011-03-03T17:47:00.000+13:00",
        "status": "Active",
        "wktGeometry": "SRID=27200;POINT (2485362.087794318 5737151.779842964)",
        "eventCreated": "2011-03-03T17:49:29.260+13:00",
        "eventModified": "2012-08-15T17:52:54.210+12:00",
        "informationSource": "NMC",
        "supplier": "Official"
    },
    "roadevent": {
        "alternativeRoute": "-",
        "directLineDistance1": "0.69 km southwest of Hakataramea",
        "directLineDistance2": "0.92 km northeast of Kurow",
        "directLineDistance3": "6.43 km southeast of Lake Waitaki",
        "eventComments": "Speed Restriction In Place For Heavy Vehicles Of 20kph.",
        "eventDescription": "Other",
        "eventId": "62595",
        "eventIsland": "South Island",
        "eventType": "Road Hazard",
        "expectedResolution": "Until further notice",
        "impact": "Caution",
        "locationArea": "SH 82 Waitaki River Bridge No 1 ( Kurow Bridges)",
        "locations": {
            "location": "082-0053/16.68   -"
        },
        "planned": "false",
        "startDate": "2012-06-14T12:17:00.000+12:00",
        "status": "Active",
        "wktGeometry": "SRID=27200;POINT (2310306.0879597953 5605922.516155325)",
        "eventCreated": "2012-06-14T12:17:55.127+12:00",
        "eventModified": "2012-08-15T17:46:26.197+12:00",
        "informationSource": "NMC",
        "supplier": "Official"
    },
Sign up to request clarification or add additional context in comments.

5 Comments

How are you able to make it so its roadevent[1] , roadevent[2] and so on, automatically. I tried to do this with the "data", by putting "[". Do I need to go: "roaddata": [{ . Thanks
Yes, it's "roadevent": [{...}, {...}, {...}].
@user2465706 : {"data": [ {"roadevent": roadevent_1_objects} , {"roadevent": roadevent_2_objects} , {...} , {"roadevent": roadevent_n_objects} ] }
@staticVoidMan that would produce: data[0].roadevent, data[1].roadevent. right?
@J.Romero : yes, but yours works as well but if there was a need for multiple roadevents for, say, different events, i would do it my way.
1
  1. just replace ,"roadevent": to ,
  2. JSON ending Gisborne Region"}}]}}
  3. JSON starting {"data":{"roadevent":[{"alternativeRoute"

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.