ManthanDalal
ManthanDalal

Reputation: 1073

How to parse JSON in PhoneGap?

I am implementing a mobile application using PhoneGap framework. I don't know how to parse JSON in PhoneGap.

Can you advise me for that?

Upvotes: 2

Views: 20030

Answers (4)

Viha Naik
Viha Naik

Reputation: 13

You can use 2 methods for parsing JSON Data

  1. var parsedData = $.parseJSON(yourJSONdata);
  2. var parsedData = JSON.parse(yourJSONdata);

Upvotes: 0

Phonegap let´s you call to device functions like gps with a simple javascript interface.

To parse a JSON object you can try this two ways:

Clean example,

  1. first, include jquery, will be more easy

  2. make a button with id = "searchButton", a div with id="dataParsed" and a textbox with id="searchBox"

  3. include this code:

    $('#searchButton').click(function() {
    $.getJSON('http://api.alternativeto.net/software/'+$('#searchBox').val()+'/?count=15', 
    function(data) {
    var items=data.Items;
    
    // uncomment the line to see the structure of the json
    //console.log(items);
    
    var list = $('#dataParsed');
    list.html("");
    
       $.each(items, function(key, val) {
           list.append($(document.createElement('p')).html(val.Name));
       });
    
    });
    
    });
    

Example of json+ajax+ jquerymobile: Read this in the phonegap wiki:

http://wiki.phonegap.com/w/page/36868306/UI%20Development%20using%20jQueryMobile

Good Luck! :)

Upvotes: 6

Simon Brodtmann
Simon Brodtmann

Reputation: 141

This is more a JavaScript than a PhoneGap question.

It should be safe to use JSON.parse(myJsonString) since no old browsers are going to be used with PhoneGap.

Upvotes: 7

alonisser
alonisser

Reputation: 12068

actually I think you are little confused, phonegap doesn't let you parse json, you parse json with javascript (like crokford json parser library or jquery json parsing methods) the only thing phonegap does is expose the native api of various mobile operation systems to a common javascript interface.

Upvotes: 2

Related Questions