views:

84

answers:

2

I need some way to either -

remove the square brackets when either saving via curl or decoding the json text file

and or remove both "created" and "modified" strings so that I am left with just the url string please when saving using curl.

[{  
"created":"10:30pm 5 August 2010",  
"url":"\/Images\/Temp\/7553-12a40d5af00-12a45a200-12acd2ff1.0.png",  
"modified":"12:00am 7 August 2010"  
}  
]  

Is there a simple way to identify each of the 3 complete strings?

What I am trying to do is eventually match the url from the script with my server url
Thanks

Ok, using Alan Storm's method I need to be able to remove the "" and string(**) from the result so that I can add http://somesite.com to the outputted url

This is the result

string(46) "/Images/temp/7553-12a4b226700-12a4b88690e.0.png"  
string(58) "/Images/temp/7553-12a488f3900-12a4c6bfe00-12a49861587.0.png"   
string(58) "/Images/temp/7553-12a488f3900-12a4eff2c00-12a4986463c.0.png"   
string(58) "/Images/temp/7553-12a488f3900-12a51925a00-12a49877738.0.png" 

Ideally this is the result that I want

http://somesite.com//Images/temp/7553-12a4b226700-12a4b88690e.0.png

this is the code that I am now using

            $txt_file = $images_dir.'iso'.$i.'.txt';
            if(file_exists($txt_file)==false)
            $img = $error_img;
        else
        {
            $handle = fopen($txt_file, 'r');
            $obj = fread($handle,filesize($txt_file));
            $array_of_objects = json_decode($obj);
            $object = $array_of_objects[0];
            var_dump($object->url); 
}

Thanks for your help so far

A: 

Assuming you need the PHP code, and you have that response in a variable named $response:

$obj = json_decode($response);
$url = $obj[0]->url;

Edited to correct the previous wrong code (pointed out by Alan)

gustavogb
Thanks - I'll give that a go.
Gary
That code isn't going to work. PHP doesn't allow array access ([]) on function calls, and I'm pretty sure array access for stdClass objects is also disallowed, or at least discouraged.
Alan Storm
Thank you Alan, it was just a quick answer explaining the idea more than working code. I updated and tested the code.
gustavogb
Yes you are correct, thanks all the same
Gary
Tried this but keep on getting errors and nulls
Gary
+1  A: 

PHP comes with built in functions for decoding JSON strings. The following code example should give you an idea of how to get at the data you want.

<?php
$string = '[{  
"created":"10:30pm 5 August 2010",  
"url":"\/Images\/Temp\/7553-12a40d5af00-12a45a200-12acd2ff1.0.png",  
"modified":"12:00am 7 August 2010"  
}  
]';

$array_of_objects = json_decode($string);
$object = $array_of_objects[0];

var_dump($object->created);
var_dump($object->url); 
var_dump($object->modified);    
Alan Storm
Thanks - this has given me 2 different ways to approach this
Gary
Have edited my original post to show what is happening with this method
Gary
Just remove the var_dump
Alan Storm
Did just that and have it working nicely thanks.
Gary