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.
Related
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
I have to create a custom json object (I need to decide what info goes where in what order) with data I get from PHP in my Controller.
As I see it, I think I need some roundtrips like this:
Get data from Laravel-form in my Controller (Input::get('name'))
Pass that data to JavaScript to build my JSON
Pass that JSON back to PHP
Convert this JSON to a string (to store it in Redis)
And when I try to read this data go the other way around:
Get data from DB
Pass data to JavaScript to get each element/node
Parse those values to html (to display the data from the json)
How would I pass variables from PHP to JS and back? Not sure how that works... A good explanation (with some code or example) would be much appreciated!!!
Or is there another 'better' way?
I am using Laravel 5.2 and NoSQL Redis. I need to create a somewhat complex JSON that looks like this and store that in Redis. This JSON will never be more than twice the size of this one shown here.
Thanks
to generate json data, you can do that with json_encode function in php. No need to pass to javascript to build json data.
http://php.net/manual/en/function.json-encode.php
update:
you can store the response of json_encode function in a variable and you can do whatever you want with that variable
$json_data=json_encode($data);
I have a web app in which I allow some large text entry using text fields. This text is saved to a database and then later it is sent back to the user as a field in a JSON response. In the browser, I attempt to simply convert it to an Object using JSON.parse, but this sometimes fails depending on what the user put in the field.
I think that right now, the text has single quotes in it, and those are breaking the browser-side Javascript before I can call JSON.parse on it.
What's the best way to sanitize this data so that, ideally, I can just parse it back to an Object with minimal cleansing after it has been saved?
This isn't a sanitization problem : you can very well put a string with quotes in JSON : the encoding simply escapes them.
Your problem is an encoding one. To build a JSON string in a browser, use JSON.stringify. To do it server side, you should use the tool provided by your (unmentionned) server side language/framework.
The awesome thing with JSON is that you do not need to sanitize anything. No matter what you feed to a JSON encoder - it will always output plain JSON. Obviously that JSON needs to be HTML-encoded in case you plan to use it within a HTML page. Depending on the JS encoder you need to ensure there's no </script> in there (e.g. by replacing / with \/).
You also do not need JSON.parse. JSON is a subset of JavaScript so you can do something like that (PHP-ish for simplicity):
<script>
var obj = <?= json_encode($whatever) ?>;
</script>
If you really want to include JSON as as tring inside JSON consider not doing it. You can just have the object itself there - no need to have a JSON string within your JSON data. But if you have this anyway it should also always work.
I have a question.
If i have a model named Input. It contains model Invoice and Collection InvoiceDetailCollection.
I modified the backbone.js specifically create:"POST" into create:"PUT" inorder to allow PUT since my service doesn't use POST.
If i were to use Input.save() What should my server expect as a request? I mean already have set a service method to expect string since if i expect string i can't use the input.save();
What should be the right parameter i should expect on my server side if i were to use the Input.save() method of backbone.js
By default, Backbone sends application/json data and the server should expects JSON data. You'll want to decode it. I use PHP and middleware that automatically json_decode($data) and turns it into an associative array for me to manipulate.
If this isn't possible, (can't take JSON data) I think what you want is to use emulateJSON It will serialize your data and send it as application/x-www-form-urlencoded like an HTML form.
http://documentcloud.github.com/backbone/#Sync-emulateJSON
I'm considering creating a simple remote debugging application for Javascript. Actually, I want to provide an object to Firebug Lite and let it do all the job.
So, can I get an object from one page, serialize it, send it to server and then to another browser and finally see the same results in Firebug Lite (on that other client) as I would see on the first browser (with doing "console.dir(obj)")? Is it possible to do?
Plain answer: no. You'll have to serialize your object to some kind of string. It could be XML, or JSON, or a format you make up, like:
var anObject = {first:1,second:2,third:'infinite'};
function serializer(obj){
var serialized = [];
for (var l in obj){
if (obj.hasOwnProperty(l)){
serialized.push(l+'='+obj[l]);
}
}
return serialized.join('&');
}
alert(serializer(anObject)); //=>first=1&second=2&third=infinite
If your object contains objects, you could use the serializer function recursively.
Using JSON for encoding the object?
http://json.org/
The easiest solution is to serialize to JSON. However, it is important to note JSON does not support all JavaScript types.
Instead of just half-answering the question, here's the real deal!
Like the others said, use JSON (implementation details) to serialize your data (because it's nativly supported by Javascript and it's lightweight), and then send it to your server using AJAX, maybe by sending it to a PHP script that just saves it to a file or a database or something.
Then on the other side, you simply receive it by again using AJAX to ask said PHP script to return that data to you!