How to make a HTML5 Geolocation object in python? - javascript

I'm testing for some software analytics and I need to send GET requests with geolocation object with variable timestamps and locations. The site uses HTML5 navigator.getcurrent.location().
I can use random module to randomise locations & timestamps. However, the JSON object creation part is troubling me as the structure in documentation has null values and python dictionary doesn't accept it.
So, could you show me how to Just create an HTML5 Geolocation object in python which is ready to be sent via GET request?

I'm assuming you're using eval to parse your JSON because you said python doesn't accept the null keyword. Using eval on JSON is asking for trouble in any language. Python has a built in JSON parser. Use it!
import json
# put GET response in location_json_string
location_dict = json.loads(location_json_string)
To encode:
# Put your data in data_dictionary
request_json = json.dumps(data_dictionary)
# send GET request appending request_json to the URL

Related

Inconsistency between Android and JavaScript Parse API

I'm using Parse Server as a back end (the open source version). I have an Android application that saves an object to the server's DB. The object is simply a key-value pair. When the Android application creates an object, it creates it as JSON object.
Now I want to retrieve the same object from an Ionic 2 app. In Ionic, I use the JS Parse API to access the back end. When an object is created in the JS API, it's created as a plain text object.
As a result I'm getting: 101 Object not found
It seems like what I need to do is to translate Parse plain text object to Parse JSON object.
This is what I tried to do:
According to the JS Parse documentation one of the methods of Parse.Object is toJSON(). I tried to apply this method in different combinations to the Parse object before creating query, but I always get an error: this.parse.Object.extend(...).toJSON is not a function
I tried to apply standard JSON Api (i.e. JSON.stringify) to the Parse object. In this case I'm getting an error: ParseQuery must be constructed with a ParseObject or class name.
I did very extensive search on internet and found nothing helpful.
Would appreciate any ideas on the matter.
Figured it out. The problem was corrupted disk on the server side. Created new server and new DB and everything works fine now.

Is there an online tool that grabs XML from some remote URL then provides a way to get the data via json using javascript?

Its easy to grab XML directly of your own domain from some local URL, but cross-domain isn't. How would you grab the XML data that is found at http://google.com/complete/search?output=toolbar&q=keep+skat with javascript?
Yes, you can! I would use YQL (Yahoo Query Language) and its pretty simple. You can definitely convert from XML to JSON and also can pass callback function to use along with JSONP.
Please visit YQL console and you can build your query from there.
For your problem, use this REST call (which I used to generate from YQL console):
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22http%3A%2F%2Fgoogle.com%2Fcomplete%2Fsearch%3Foutput%3Dtoolbar%26q%3Dkeep%2Bskat%22&format=json&diagnostics=true&callback=myCallbackFunc
Response from this URL will give you the JSON from the XML returned from your URL.

Python and a JSON Array

I am having problems with Python (Django) Framework. I at first attempted to send a Javascript Array straight to Django except it pulled only the last value. So I use now JSON.stringify which sends my JSON to Python.
At Python it looks like this
QueryDict: {u'[{"key:":"interface.dns","value:":192.168.0.1"}]':[u'']}
And if I add more values it just adds to the first U. How can I make this so its a loop.
e.g for each key value in the dictionary display it. Because it seams the JSON is being stored at position one in the Dictionary.
Any Ideas
QueryDict objects implement the file interface. So you could decode the JSON simply with the standard JSON library:
import json
...
parsed_json = json.load(request.POST) # Or GET, depends on what you are using
You can use Djangos json deserializer:
data = request.POST.keys()[0]
for obj in serializers.deserialize("json", data):
do_something_with(obj)
Read more.
It seems that your json is sent as a key instead of a value in the POST (or GET) request. This can work but for convention it should be in the value part.

How to create a JQuery or Javascript Array from HTML Formatted HTTP Response?

I'm using a service called TrueFX that provides currency information based on HTTP request/response. I need to send the initial request to the base URL with username, and other parameters. The service responds with a session ID. A subsequent HTTP request is sent with the session ID and the instructions for output type (CSV or HTML). The second response provides an HTML table with the requested data.
The final destination for this data will be a chart via the JQPlot plugin for JQuery. JQPlot will accept an array instead of individual values to be plotted.
Questions:
Is it best to create the array of data using JQuery, plain ole Javascript or PHP?
Depending on response to above, how do I define the request to TrueFX, the TrueFX response containing currency data in HTML format? Is this just done through the $.ajax JQuery method?
How do I create and save the array for reference and use in my call to JQPlot?
Thanks so much for your advice, help and guidance.
First off I would recommend you work with the CSV format as it will be much easier to parse into an array of arrays for use in your chart (also data size will be much smaller). There are multiple CSV plugins for jquery that make this parsing pretty easy. Here is one I found real quick: CSV Plugin
You can use jquery's $.ajax to get the data from the remote service (using JSONP) and the CSV plugin to parse the returned data. Then you feed the resulting array of arrays into your chart.

YUI "Get" utility to parse JSON response?

The documentation page for the YUI "Get" utility says:
Get Utility is ideal for loading your
own scripts or CSS progressively
(lazy-loading) or for retrieving
cross-domain JSON data from sources in
which you have total trust.
...but doesn't have any actual examples for how to do so. Their one example doesn't actually request a JSON document from a remote server, but instead a document containing actual JavaScript along with the JSON data.
I'm just interested in the JSON response from the Google Maps API HTTP (REST) interface. Because I can't do cross-site scripting with the "Connect" utility, I am trying the "Get" utility. But merely inserting some JSON data into the page isn't going to do anything, of course. I have to assign it to a variable. But how?
Also, just inserting JSON data into the page makes Firefox complain that there's a JavaScript error. And understandably! Plain ol' JSON data isn't going to parse as valid JavaScript.
Any ideas?
Normally in this case the easiest thing to do is to return javascript that calls a callback with the json. For example:
function xdCallback( json ) {
// here I can do whatever I need with json, maybe
SomeModule.heresTheJson( json );
// or
globalVar.json = json;
// etc
}
And so on your server side you return not just JSON but instead something like:
xdCallback( { json: 'goes', here: true } );
...execute the 'script' when you get it via your ajax call and you're set.
OK. Looks like without Google's HTTP Geocoding interface supporting JSONP, there is no way to do this. :(
Sean -- You may find that YUI Connection Manager's XDR support is what you're looking for --
http://developer.yahoo.com/yui/examples/connection/xdr.html (YUI 2)
http://developer.yahoo.com/yui/3/examples/io/io-xdr.html (YUI 3)
Use Connection Manager (YUI 2) or IO (YUI 3) to bring in the JSON, and then use the JSON component in either codeline to parse the JSON once it's loaded.
If Google or Yahoo! has the necessary cross-domain support on the relevant servers, you should be in business.
-Eric
I have used YAHOO.lang.JSON.parse to parse a string to json. Also the stringify method can be used to go from JSON back to a string:
http://developer.yahoo.com/yui/docs/YAHOO.lang.JSON.html

Categories

Resources