2

I am grabbing data from a Google spreadsheet through the Google API using cURL in PHP. Using a AJAX HTTP request (via jQuery) I can pull all the data in and get it into an array, but since the <content> tag looks like dirty JSON I'm a little stuck.

I would like to be able to reference the data as a JS object, like so:

alert(xml.feed.content.name);

Example Code:

$.ajax({
   type: "GET",
   url: GtargetURL,
   dataType: "xml",
   success: function parseMyXML(xml){
      var Entries = new Array;
      var i = 0;
      $(xml).find("entry").each(function(){
         var content = $(this).find("content").text();
         Entries[i]=content;
         i++;
      });
      var myArray= new Array();
      myArray= Entries[1].split(",");
      alert (myArray[1]); // Result: "test2"
   }        
});

Example XML:

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gsx=
<entry>
<content type='text'>relativeid: 4, name: test2, type:  teset3, multiples: yes, cat: yes</content>
</entry>
<entry>many more entries...</entry>
</feed>

Thanks for any help you can offer.

For what it's worth, I am using this URL format for the Google api call:

https://spreadsheets.google.com/feeds/list/KEY-ID-HERE/1/public/basic

I know that I can do a "cell" call instead of a "list" call, but this suits my purposes better.

Matt
  • 74,352
  • 26
  • 153
  • 180
iammatthew2
  • 639
  • 2
  • 7
  • 20

2 Answers2

3

You could do something like this: http://jsfiddle.net/GVUnF/ http://jsfiddle.net/rMMkD/1/ in your loop.

var jsonLikeString = "name:red, type:blue, multiples:green, cat:brown";

var jsObject = {};

var stringWithoutSpaces = jsonLikeString.split(' ').join('');
var splitStrings = stringWithoutSpaces.split(",");
var kvPairArray = [];

for(var i in splitStrings){
    if(splitStrings.hasOwnProperty(i)){     
        var kvPair = splitStrings[i];
        kvPairArray = kvPair.split(":");
        jsObject[kvPairArray[0]] = kvPairArray[1];

    }
}
alert(jsObject.cat);

Please note that

var foo = new Array;

is not exactly idiomatic in javascript. You should use

var foo = [];

instead.

Also, for appending to an array you should use

foo.push('something');

instead of having a variable i and incrementing it every loop.

Tim
  • 2,430
  • 19
  • 20
  • This answer is nice and easy, but it's not quite working. In this Fiddle you can see the "undefined" popup: http://jsfiddle.net/ZspQr/ – iammatthew2 Feb 15 '12 at 22:47
  • The problem was that there were still spaces in the string, so all except the first keys had a leading space. http://jsfiddle.net/rMMkD/1/ this fixes it. – Tim Feb 15 '12 at 23:38
  • Thanks! Combined with a xml to JSON plugin this will do the job. Much appreciated! – iammatthew2 Feb 15 '12 at 23:42
  • By the way, it might be better to check if there are spaces at the beginning of the keys within the loop instead of removing them for the entire string. You might receive data that contains strings with spaces. – Tim Feb 16 '12 at 00:10
  • Please include the relevant code from the fiddle as part of your answer. – Benjamin Gruenbaum Feb 16 '14 at 08:30
3

There are two parts to your question:

  • How to turn XML into JSON
  • How to turn some text in XML that is almost JSON into JSON

To make XML into JSON you can use a library like http://www.thomasfrank.se/xml_to_json.html

It turns

<animals>
     <dog>
          <name>Rufus</name>
          <breed>labrador</breed>
     </dog>
     <dog>
          <name>Marty</name>
          <breed>whippet</breed>
     </dog>
     <cat name="Matilda"/>
</animals>

Into

{"animals":
    {"dog":[
        {"name":"Rufus",
         "breed":"labrador"},
        {"name":"Marty",
         "breed":"whippet"}],
     "cat":
        {"name":"Matilda"}}}

Then you can follow the suggestion from TheShellfishMeme to turn

relativeid: 4, name: test2, type:  teset3, multiples: yes, cat: yes

Into a JSON object

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Thanks for the advice. I noted that the other response answered my question, but yous helped. I ended up using this plugin: http://www.fyneworks.com/jquery/xml-to-json/ – iammatthew2 Feb 15 '12 at 23:45