从JSON字符串除去双引号(闭合)(Removing Double Quotes from JSON

2019-08-31 12:16发布

我正在一个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" :[字符串]?

难道我做的XSL? 在PHP? 谢谢。

Answer 1:

自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



Answer 2:

您JSON-编码SimpleXMLElement它默认返回元素节点值的字符串

如果你想改变这种行为,你需要从它扩大和改变它编码的JSON对象的方式,例如阵列(如果存在的话)应该被转换为float值:

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

就是这样了。



Answer 3:

关于什么

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


文章来源: Removing Double Quotes from JSON String [closed]
标签: php xml json xslt