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='' xmlns:openSearch='.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:
I know that I can do a "cell" call instead of a "list" call, but this suits my purposes better.
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/2005/Atom' xmlns:openSearch='http://a9./-/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./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.
Share Improve this question edited Feb 15, 2012 at 19:26 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Feb 15, 2012 at 19:08 iammatthew2iammatthew2 6592 gold badges7 silver badges21 bronze badges 2- See this article? stackoverflow./questions/1773550/… – crush Commented Feb 15, 2012 at 19:10
- How about: webapps.stackexchange./questions/11864/… – Diodeus - James MacFarlane Commented Feb 15, 2012 at 19:13
2 Answers
Reset to default 3You could do something like this: http://jsfiddle/GVUnF/ http://jsfiddle/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.
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140812a4613421.html
评论列表(0条)