Removing Double Quotes from JSON String [closed]

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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>

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>

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

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

This is my current JSON output

{"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"]}]} 

This is what I need my JSON output to look like. I need to remove the double quotes ("")that are around the 0.00 values.

{"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]}]} 

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

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

回答1:

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.

Edit:

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



回答2:

You json-encode a SimpleXMLElement which by default returns the element node values as strings.

If you want to change this behavior, you need to extend from it and change the way it encodes the object for json, e.g. the rain array (if it exists) should be converted to float values:

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

Your script then only needs the little change to hint the loading function to use the class with the changed serialization behavior:

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

And that's it already.



回答3:

what about

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!