JSON with comma

122 views
Skip to first unread message

Charles Wilt

unread,
Dec 18, 2015, 8:29:21 AM12/18/15
to DroidScript
I am reading json info that has a comma in one field. When parsing these the comma acts like a carraige return. How can I deal with this? Any tips?

Steve Garman

unread,
Dec 18, 2015, 9:16:24 AM12/18/15
to DroidScript
Charles,

are you saying that something like this comes out wron?


    var j= '{"a":"aaa","b":"b,b","c":"ccc"}'
    var obj = JSON.parse(j);
    app.ShowPopup(obj.b);

Charles Wilt

unread,
Dec 18, 2015, 5:54:03 PM12/18/15
to DroidScript
"title": "M 5.4 - 50km NNE of Achham, Nepal"

Becomes 2 lines:
M 5.4 - 50km NNE of Achham
Nepal

I need to parse that as a single line.

Steve Garman

unread,
Dec 18, 2015, 6:17:28 PM12/18/15
to DroidScript
Charles,
this still works OK for me

    var j = '{"title":"M 5.4 - 50km NNE of Achham, Nepal"}'
    var obj=JSON.parse(j);
    app.ShowPopup(obj.title);

How are you getting the json into your app? My guess is it is being corrupted on the way in.

Are you creating an intent through some sort of shell?
Shells are notorious for partially parsing json so it may need some form of escaping.

Charles Wilt

unread,
Dec 18, 2015, 7:37:08 PM12/18/15
to DroidScript
    The call...

    var USGS_day_Request = new XMLHttpRequest(); 
    USGS_day_Request.onreadystatechange = function() { USGS_day_Reply(USGS_day_Request); };   
    USGS_day_Request.open("GET", url, false); 
    USGS_day_Request.send(null);   

The return..

//Handle the USGS day reply 
function USGS_day_Reply( USGS_day_Request ) 
    if( USGS_day_Request.readyState==4 ) 
    { 
        //If we got a valid response. 
        if( USGS_day_Request.status==200 ) 
        { 
            var message="";
            //Parse JSON
            USGS_day=JSON.parse(USGS_day_Request.responseText);
            message="USGS 4.5+ Dayly Report";
            message+="Total : "+USGS_day.metadata.count+"\n";
            
            //Add to Seismic text display.
            seismic_day_txt.SetText(message);
        } 
        //An error occurred 
        else 
           app.Alert( "USGS day Error: " + USGS_day_Request.status + USGS_day_Request.responseText); 
    } 
}

Produces lines broken at the comma.

Steve Garman

unread,
Dec 18, 2015, 8:35:11 PM12/18/15
to DroidScript
I get an error page when I try your code so I can't test it.

But are you sure you need the JSON.parse() in your success handler?

The HTTP Get sample that comes with DroidScript claims to be processing json and manages without the JSON.parse()

The error I get is:
USGS day Error: 404<!DOCTYPE html>
<!--[if lt IE 7]> <html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="ie ie8" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
<head>
<title>Oops! &mdash; File Not Found (404)</title>
<!-- 151217,221901,1 -->
<script type="text/javascript" src="/template/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/template/js/master_20150623.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="keywords" content="aftershock,earthquake,epicenter,fault,foreshock,geologist,geophysics,hazard,hypocenter,intensity,intensity scale,magnitude,magnitude scale,mercalli,plate,richter,seismic,seismicity,seismogram,seismograph,seismologist,seismology,subduction,tectonics,tsunami,quake,sismologico,sismologia"/>
<meta name="google-site-verification" content="5Bw3LIEvwx8C0JA5hWKosAweW7kXIw0LS5OR438MOqU" />
<meta name="description" content="USGS Earthquake Hazards Program, responsible for monitoring, reporting, and researching earthquakes and earthquake hazards"/>

<!--[if lt IE 7]>
<link rel="stylesheet" href="/template/css/ietextonly.css" type="text/css" media="all" />
<![endif]-->

<!--[if gte IE 7]><!-->
<link rel="stylesheet" type="text/css" media="all" href="/template/css/reset.css"/>
<link rel="stylesheet" type="text/css" media="all" href="/template/css/master.css"/>
<!--<![endif]-->

<link rel="stylesheet" media="print" type="text/css" href="/template/css/print.css" />

<!--[if gte IE 7]><!--><link rel="stylesheet" type="text/css" href="/template/widgets/ui/ui.css"/><style type="text/css">dl.causes{margin:2em 0;}.causes dt{font-weight:bold;margin-top:1em;}.causes dd{margin-left:40px;}</style><!--<![endif]--></head>

<body>

<div id="header"><div id="usgsbanner"><div>
<a href="http://www.usgs.gov/"><img src="/template/images/usgs.jpg" alt="USGS - science for a changing world" title="U.S. Geological Survey Home Page" /></a>
<ul>
<li><a href="http://www.usgs.gov/">USGS Home</a></li>
<li><a href="http://answers.usgs.gov/cgi-bin/gsanswers?tmplt=2/">Contact USGS</a></li>
<li><a href="http://search.usgs.gov/">Search USGS</a></li>
</ul>
</div></div>

<div id="usgstitle"><div>
<p><a href="/">Earthquake Hazards Program</a></p>
<ul id="utilities">
<li class="invisiblelink"><a href="#startcontent" class="invisiblelink">Skip to main content</a></li>
<li><a href="/">Home</a></li><li><a href="/aboutus/">About Us</a></li><li><a href="/contactus/">Contact Us</a></li> <li id="search">

<form method="get" action="http://www.google.com/search" id="header-search-form" target="_blank" class="search-form">
<span class="search-wrap">
<input type="text" name="q" size="10" maxlength="50" title="Search Text" class="search-text" />
<input type="image" src="/template/images/sbox_button.gif" alt="Search" class="search-button" />
</span>
<input type="hidden" name="q" value="site:earthquake.usgs.gov" class="search-siteonly" />
</form>
</li>
</ul>
</div>
</div>

</div>

<hr />
<div id="sitenav"><ul><li><a href="/earthquakes/?source=sitenav">Earthquakes</a></li><li><a href="/hazards/?source=sitenav">Hazards</a></li><li><a href="/data/?source=sitenav">Data<span style="font-size: .85em;"> &amp; </span>Products</a></li><li><a href="/learn/?source=sitenav">Learn</a></li><li><a href="/monitoring/?source=sitenav">Monitoring</a></li><li><a href="/research/?source=sitenav">Research</a></li></ul>
</div>
<hr />

<div id="content">
<a id="startcontent"></a>

<h1><span class="error">Oops!</span> &mdash; File Not Found (404)</h1>


<dl class="causes">

<dt>Did you follow a link on our website?</dt>
<dd>
If you reached this page from another part of earthquake.usgs.gov,
please use our <a href="/contactus/">Contact Form</a> so we can correct our mistake.
</dd>

<dt>Did you follow a link from another website?</dt>
<dd>
Please check our <strong>site map</strong> at the bottom of any of our webpages
or <a href="/search/">Search our website</a> to find the page's new location.
Do not send us an email about the broken link because it's not on our site, and we can't fix it.
</dd>

<dt>Did you type the URL?</dt>
<dd>You may have typed the address (URL) incorrectly. Check to make sure you have the correct spelling, capitalization, etc.</dd>

</dl>
</div>

<hr />

<div id="content_footer">

</div>
<hr />

<div id="sitemap">
<ul>
<li>
<a href="/earthquakes/?source=sitemap" class="first">Earthquakes</a>
<ul>
<li><a href="/earthquakes/map/">Latest Earthquakes</a></li>
<li><a href="/earthquakes/feed/">Real&#45;time Feeds &amp; Notifications</a></li>
<li><a href="/earthquakes/search/">Search EQ Archives</a></li>
<li><a href="/earthquakes/region.php">Info by Region</a></li>
<li><a href="/earthquakes/world/seismicity_maps/">World Seismicity Maps</a></li>
<li><a href="/earthquakes/eqarchives/">&ldquo;Top 10&rdquo; Lists &amp; Maps</a></li>
<li><a href="/earthquakes/dyfi/">Did You Feel It?</a></li>
<li><a href="/earthquakes/shakemap/">ShakeMap</a></li>
<li><a href="/earthquakes/pager/">PAGER</a></li>
<li><a href="/earthquakes/eqinthenews/">Significant EQ Archive</a></li>
</ul>
</li>
<li>
<a href="/hazards/?source=sitemap">Hazards</a>
<ul>
<li><a href="/hazards/products/">Hazard Maps &amp; Data</a></li>
<li><a href="/hazards/designmaps/">Seismic Design</a></li>
<li><a href="/hazards/apps/">Hazard Analysis Tools</a></li>
<li><a href="/hazards/qfaults/">Faults</a></li>
</ul>
</li>
<li>
<a href="/data/?source=sitemap">Data<span style="font-size: .85em;"> &amp; </span>Products</a>
<ul>
<li><a href="/data/">Data</a></li>
<li><a href="/data/products/">Products</a></li>

</ul>
</li>
<li>
<a href="/learn/?source=sitemap">Learn</a>
<ul>
<li><a href="/learn/topics/">EQ Topics for Education</a></li>
<li><a href="http://www.usgs.gov/faq/?q=taxonomy/term/9762"><abbr title="Frequently Asked Questions">FAQ</abbr></a></li>
<li><a href="/learn/glossary/">EQ Glossary</a></li>
<li><a href="/learn/kids/">For Kids</a></li>
<li><a href="/learn/kml.php">Google Earth/KML Files</a></li>
<li><a href="/earthquakes/eqarchives/poster/">EQ Summary Posters</a></li>
<li><a href="/learn/photos.php">Photos</a></li>
<li><a href="/learn/publications/">Publications</a></li>
</ul>
</li>
<li>
<a href="/monitoring/?source=sitemap">Monitoring</a>
<ul>
<li><a href="/contactus/golden/neic.php">NEIC</a></li>
<li><a href="/monitoring/anss/"><abbr title="Advanced National Seismic System">ANSS</abbr> &ndash; United States</a></li>
<li><a href="/monitoring/gsn/"><abbr title="Global Seismic Network">GSN</abbr> &ndash; World</a></li>
<li><a href="/monitoring/volunteer/">Volunteer Monitoring</a></li>
<li><a href="/regional/asl/"><abbr title="Albuquerque Seismo Lab">ASL</abbr> &ndash; Albuquerque</a></li>
<li><a href="/monitoring/operations/">Network Operations</a></li>
<li><a href="/monitoring/helicorders.php">Seismogram Displays</a></li>
<li><a href="/monitoring/buildings/">Buildings</a></li>
<li><a href="/monitoring/nsmp/"><abbr title="National Strong Motion Project">NSMP</abbr> &ndash; Strong Motion</a></li>
<li><a href="/monitoring/deformation/">Crustal Deformation</a></li>
</ul>
</li>
<li>
<a href="/research/?source=sitemap">Research</a>
<ul>
<li><a href="/research/faults/">Characterizing Faults</a></li>
<li><a href="/research/earlywarning/">Early Warning</a></li>
<li><a href="/research/deformation/">Crustal Deformation</a></li>
<li><a href="/research/eqproc/">Earthquake Processes</a></li>
<li><a href="/research/groundfailure/">Ground Failure</a></li>
<li><a href="/research/groundmotion/">Ground Motion</a></li>
<li><a href="/research/hazrisk/">Hazard &amp; Risk Assessment</a></li>
<li><a href="/research/monitoring/">Improving Monitoring</a></li>
<li><a href="/research/induced/">Induced Earthquakes</a></li>
<li><a href="/research/external/">External Support</a></li>
<li><a href="/research/index.php">Regional Science Activities</a></li>
<li><a href="/research/software/">Software</a></li>
</ul>
</li>
</ul>
</div>

<hr />

<div id="usgsnav">
<ul>
<li><a href="http://www.usgs.gov/laws/accessibility.html" title="Accessibility Policy (Section 508)">Accessibility</a></li>
<li><a href="http://www.usgs.gov/foia/"><abbr title="Freedom of Information Act">FOIA</abbr></a></li>
<li><a href="http://www.usgs.gov/laws/privacy.html" title="Privacy policies of the U.S. Geological Survey.">Privacy</a></li>
<li><a href="http://www.usgs.gov/laws/policies_notices.html" title="Policies and notices that govern information posted on USGS Web sites.">Policies and Notices</a></li>
<li><a href="http://www.nehrp.gov" class="nehrp">In partnership with <abbr title="National Earthquake Hazards Reduction Program">n<span class="e">e</span>hrp</abbr></a></li>
<li><a href="http://www.usa.gov/" class="usagov" title="USAGov: Government Made Easy"><img src="/template/images/usagov.jpg" alt="USA.gov" width="90" height="26" /></a></li>
</ul>
</div>
<div id="footer">
<p>
<a href="http://www.doi.gov/index.cfm">U.S. Department of the Interior</a> | <a href="http://www.usgs.gov/">U.S. Geological Survey</a><br />
Page URL: http://earthquake.usgs.gov/notfound.php<br />
Page Contact Information: <a target="_blank" href="mailto:sis_eq_q...@usgs.gov?subject=EHP%20Website%20Email%20">Contact Us</a><br />
Page Last Modified: January 16, 2014 17:46:10 UTC </p>
<small class="disclaimer"><img src="/template/images/small_offsite_arrow.gif" alt="Offsite Link" title="Offsite Link"> <a href="http://www.doi.gov/disclaimer.cfm" title="Department of the Interior - Disclaimer of Liability and Endorsement">DOI</a> and <a href="http://www.usgs.gov/laws/info_policies.html" title="USGS - Information Policies">USGS</a> link policies apply.</small></div>
<script type="text/javascript" src="/contactus/js/ContactValidator.js"></script><script type="text/javascript" src="/contactus/js/FAQFilter.js"></script><script type="text/javascript" src="/contactus/js/contactus.js"></script><script type="text/javascript" src="/template/widgets/ui/ui.js"></script><script type="text/javascript" src="/template/js/lib.20080317.min.js"></script>
<!--[if lt IE 7]><script type="text/javascript" src="/js/minmax.js"></script><![endif]-->
<!--[if lt IE 8]><script type="text/javascript" src="/template/js/offsite.js"></script><![endif]-->


</body>
</html>

Charles Wilt

unread,
Dec 18, 2015, 9:37:57 PM12/18/15
to DroidScript
Better example code.


//Called when application is started.
function OnStart()
{
//USGS Seismic day activity request
    var USGS_day_Request = new XMLHttpRequest(); 
    USGS_day_Request.onreadystatechange = function() { USGS_day_Reply(USGS_day_Request); };   
    USGS_day_Request.open("GET", url, false); 
    USGS_day_Request.send(null);   
}

function USGS_day_Reply( USGS_day_Request ) 
    if( USGS_day_Request.readyState==4 ) 
    { 
        //If we got a valid response. 
        if( USGS_day_Request.status==200 ) 
        { 
            var message="";
            //Parse JSON
            USGS_day=JSON.parse(USGS_day_Request.responseText);
            message="USGS 4.5+ Dayly Report";
            message+="Total : "+USGS_day.metadata.count+"\n";
            
            //Add to Seismic text display.
            var temp,x;
            for (var key in USGS_day.features)
            {  
                x=USGS_day.features[key].properties.title;
                x.replace(','," - ");
                //key+=1;
                temp+=x;
            }
    feature=app.CreateListDialog("Seismic Activity",temp);

Steve Garman

unread,
Dec 19, 2015, 1:50:24 AM12/19/15
to DroidScript
As far as I can tell, the json is being parsed correctly
It appears to be CreateListDialog that is (correctly) converting the , to an item delimiter.

You seem to have made an attempt to fix this but your code
x.replace(','," - ");
makes no difference to the data.

I think what you are looking for is
x=x.replace(','," - ");

Charles Wilt

unread,
Dec 19, 2015, 7:28:53 AM12/19/15
to DroidScript
Thanks Steve. That was it.

Couldn't see the forest for the tress. I had tried the x.replace once with no luck. x=x.replace works. Then add a new line to the end and its almost complete. Just need to remove the first "undefined"

Joelon bitmal

unread,
Dec 20, 2015, 10:42:09 PM12/20/15
to DroidScript
hi, can you help me on how to fix this.

this is the response from my server

[{"ProductID":"17","ProductTypeID":"12","SerialNumber":"00001","OwnedByOthers":"0","OwnerName":"","ProductStatus":"In","CustomerName":"","IsEmpty":"0","onRefill":"0","isDeleted":"0"}]

i have used your code, but how can i get the results like this

ProductID = 17
ProductTypeID = 12
SerialNumber = 00001
OwnedByOthers = No //0 = No, 1 = Yes

something like that

Charles Wilt

unread,
Dec 20, 2015, 10:52:38 PM12/20/15
to DroidScript
You must run the server response through json.parse function to create an object to be read.

Some pseudo code...

var MyJson=JSON.parse(ServerReturn);
app.Alert(MyJson.ProductID);

This works if there is only 1 data set.

Mikhael

unread,
Feb 12, 2020, 2:43:48 AM2/12/20
to DroidScript
Hello

Please tell me if I can use a comma in the item of ListDialog? How can I escape a comma?

Steve Garman

unread,
Feb 12, 2020, 4:34:12 AM2/12/20
to DroidScript
Please don't hijack threads with inappropriate subjects.

I couldn't find a way to escape a comma in a ListView but if you only need a basic ListView, this may do as an alternative https://groups.google.com/d/msg/androidscript/H-HS4LA8eps/ApYIUpK6FgAJ

Reply all
Reply to author
Forward
0 new messages