从JSON字符串中删除双引号 [英] Removing Double Quotes from JSON String

查看:120
本文介绍了从JSON字符串中删除双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取一个看起来像这样的xml文件

I am taking an xml file that looks like this

    <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文档.

Using this xsl style sheet to change/modify the xml document.

<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文件

Than I am using PHP to output the new xml file

<?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

and this php to output 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]?

How do I change the "rain":[string]?

我可以在xsl中执行此操作吗?在PHP中?谢谢.

Do I do it in xsl? In php? Thank you.

推荐答案

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

As of php 5.3.3 you can pass a JSON_NUMERIC_CHECK flag to json_encode that will check if a value is numeric and encode the json string with a number instead of a string.

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

Per my last comment, using string replace, this would work:

<?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

这篇关于从JSON字符串中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆