将JSON数据放入PHP中的变量中

时间:2013-03-22 14:36:22

标签: php json

编辑:我认为最终输出看起来不像JSON。如果没有,那是什么?

我有这段代码:

$url = 'http://eligibility.sc.egov.usda.gov/eligibility/eligibilityservice?eligibilityType=Property&requestString=<?xml version="1.0"?><Eligibility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/var/lib/tomcat5/webapps/eligibility/Eligibilitywsdl.xsd"><PropertyRequest StreetAddress1="'.$street.'" StreetAddress2="" StreetAddress3="" City="'.$city.'" State="'.$state.'" County="" Zip="'.$zip.'" Program="RBS"></PropertyRequest></Eligibility>';
$url_arr = explode( 'requestString=', $url );
$xml = simplexml_load_string( $url_arr[ 1 ] ); // requires allow_url_fopen to be on
$elg = (string)$xml->Property[Eligibility];

var_dump($xml);

$xml = simplexml_load_file($url); // requires allow_url_fopen to be on
$elg = json_encode((string)$xml->Property[Eligibility]);

var_dump($elg);

将这个阵列放在哪个位置:

object(SimpleXMLElement)#1 (1) {
    ["PropertyRequest"]=> object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=> array(8) {
            ["StreetAddress1"]=> string(13) "7865 ILLINOIS" 
            ["StreetAddress2"]=> string(0) "" 
            ["StreetAddress3"]=> string(0) "" 
            ["City"]=> string(10) "CASEYVILLE" 
            ["State"]=> string(2) "IL" 
            ["County"]=> string(0) "" 
            ["Zip"]=> string(5) "62232" 
            ["Program"]=> string(3) "RBS" 
        } 
    } 
} 
string(12) ""InEligible""

我只想抓住string(12) ""InEligible""部分。我怎么把它变成一个变量?

2 个答案:

答案 0 :(得分:1)

从您的代码中$elg已经拥有此值,它是一个变量。

答案 1 :(得分:0)

  

编辑:我认为最终输出看起来不像JSON。如果没有,那是什么?

string(12) ""InEligible""

那是JSON。 JSON中的字符串用引号括起来,不多也不少。