从JSON字符串中删除双引号

时间:2013-04-18 20:11:20

标签: php xml json xslt

我正在使用一个看起来像这样的xml文件

    <FCDMC><rpt_info created="data generated 04/16/2013  16:45"/><gage_rain id="770" last_rpt="2013-04-16T14:22:11" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" name="Tat Momolikot Dam" lat="032:39:04" long="111:55:41"/></FCDMC>

使用此xsl样式表更改/修改xml文档。

<xsl:stylesheet version="1.0">
  <xsl:output method="xml" encoding="utf-8" media-type="text/xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="rpt_info">
    <xsl:element name="meta" select=".">
      <xsl:for-each select="@created">
        <xsl:element name="created" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
  <xsl:template match="gage_rain">
    <xsl:element name="data" select=".">
      <xsl:for-each select="@id">
        <xsl:element name="site" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@lat">
        <xsl:element name="latitude" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@long">
        <xsl:element name="longitude" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@name">
        <xsl:element name="name" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@last_rpt">
        <xsl:element name="last_rpt" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@min_10">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@min_30">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@hour_1">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@hour_3">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@hour_6">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@day_1">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@day_3">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="@day_7">
        <xsl:element name="rain" select=".">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

比我使用PHP输出新的xml文件

<?php
header('Content-Type: application/xml');
$xml = new DOMDocument;
$xml->load('http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_rain.xml');
$xsl = new DOMDocument;
$xsl->load('http://alert.fcd.maricopa.gov/alert/Google/v3/xslt/fcdmc_alert_rain.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 
echo $proc->transformToXML($xml);
?>

和这个php输出JSON

<?php
$xml = simplexml_load_file('http://alert.fcd.maricopa.gov/alert/Google/v3/php/rainfall_data.php');
$json = json_encode($xml);
echo $json;
?>

这是我当前的JSON输出

{"meta":{"created":"04-18-2013 12:45"},"data":[{"site":"770","latitude":"032:39:04","longitude":"111:55:41","name":"Tat Momolikot Dam","last_rpt":"2013-04-18T11:22:11","rain":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]}]} 

这就是我需要JSON输出的样子。我需要删除大约0.00值的双引号(“”)。

{"meta":{"created":"04-18-2013 12:45"},"data":[{"site":"770","latitude":"032:39:04","longitude":"111:55:41","name":"Tat Momolikot Dam","last_rpt":"2013-04-18T11:22:11","rain":[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00]}]} 

如何更改"rain":[string]?

我是在xsl中做的吗?在PHP?谢谢。

3 个答案:

答案 0 :(得分:1)

你json编码SimpleXMLElement,它默认将元素节点值返回为 strings

如果要更改此行为,则需要从中进行扩展并更改为json编码对象的方式,例如: rain 数组(如果存在)应转换为浮点值:

class JsonSerializeXMLElement extends SimpleXMLElement implements JsonSerializable
{
    public function jsonSerialize() {
        $array = (array) $this;
        if ($this->rain) {
            $array['rain'] = array_map('floatval', $array['rain']);
        }
        return $array;
    }
}

您的脚本只需要稍加改动就可以提示加载函数使用具有更改的序列化行为的类:

$filename = 'http://alert.fcd.maricopa.gov/alert/Google/v3/php/rainfall_data.php';
$xml = simplexml_load_file($filename, 'JsonSerializeXMLElement');
$json = json_encode($xml);

就是这样。

答案 1 :(得分:0)

从php 5.3.3开始,您可以将JSON_NUMERIC_CHECK标志传递给json_encode,该标志将检查值是否为数字,并使用数字而不是字符串对json字符串进行编码。

编辑:

根据我的上一条评论,使用字符串替换,这将起作用:

<?php
//the json data, since I don't have the original data, I am just decoding the json output.
$json = '{"meta":{"created":"04-18-2013 12:45"},"data":[{"site":"770","latitude":"032:39:04","longitude":"111:55:41","name":"Tat Momolikot Dam","last_rpt":"2013-04-18T11:22:11","rain":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]}]}';

//decode the json output
$array = json_decode($json, 1);

//an empty array for rain data
$rain = array();

//loop through each data
foreach($array['data'] as $k=>$v){
    //save the rain data
    $rain[$k] = $v['rain'];
    //overwrite the rain data with a simple unique string that can be replaced
    $array['data'][$k]['rain'] = "{rain data {$k}}";
}

//encode the new data with the replacement string
$json = json_encode($array);

//loop over the rain data replacing the rain data replacement string with a JSON_NUMERIC_CHECK json_encoded rain data
foreach($rain as $k=>$v){
    //build the search string
    $search = '"{rain data '.$k.'}"';
    //build the replace string
    $replace = json_encode($v, JSON_NUMERIC_CHECK);
    //do the replace
    $json = str_replace($search, $replace, $json);
}
var_dump($json);

http://codepad.viper-7.com/hiWxjH

答案 2 :(得分:-1)

怎么样

<xsl:value-of select="number(RAIN_STRING_HERE)"/>