开发者

Json - removing square brackets and identifying a certain string

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 th开发者_JS百科is 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


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);    


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜