0

Is it possible to send JSON from the server to my iOS app using Python?

How is something like this implemented?

(Django or Flask is fine)

I have searched online but only found PHP tutorials.


I have tried returning JSON but it did not work - or at least it never got sent to the app

davidism
  • 121,510
  • 29
  • 395
  • 339
JoseSwagKid
  • 389
  • 1
  • 6
  • 12

4 Answers4

1

The basic premise is that your client app sends a request a URL, and the server responds with JSON.

How you create the JSON document is not that important. You can use any language (even javascript) to generate the JSON and send it back.

For django there is tastypie, django-rest and piston.

For flask you can use flask-restful. Here is a complete example.

No matter which library you use, the workflow will go like this:

  1. Your application makes a request to a URL
  2. Your server application (written in any of the above frameworks), intercepts this request.
  3. A JSON document is generated by your server application, and sent back as a response.
  4. Your client application reads the JSON and displays it.

Now, if you want to implement a REST API, then you expand on the above to allow operations from the client app. For example, a specially crafted request can add new records, delete records or query for a subset of records. However, this is not a requirement if all you want to do is send JSON documents back to your application.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thank you very very very much. I have not heard of REST before - I am still young. I will try and use REST to implement it. :) – JoseSwagKid Feb 17 '13 at 05:45
1

For flask, you can use jsonify

from flask import jsonify

@app.route("/index/", methods=['GET'])
def index():
    return jsonify(result=result)
Joe
  • 6,460
  • 1
  • 19
  • 15
0

if you use django the following might help:

#views.py
import json
from django.http import HttpResponse

def jsonview(request):
    ...
    return HttpResponse(json.dumps(result), mimetype='application/json')

better still instead of using json directly use

from django.core.serializers import serialize
serialize('json', result) #instead of json.dumps(result)
Doobeh
  • 9,280
  • 39
  • 32
Abraham
  • 21
  • 1
0

Example for the return of JSON from a django server:

from django.http import HttpResponse, HttpResponseNotFound

class JsonResponse(HttpResponse):
    def __init__(self, content, **kwargs):
        kwargs['content_type'] = 'application/json'
        super(JsonResponse, self).__init__(json.dumps(content), **kwargs)

class JsonDataResponse(HttpResponse):
    """
    Returns a json dumped dict with fields 'message' and 'data'.

    """
    def __init__(self, *args, **kwargs):
        message = None
        data = None
        if len(args) > 0:
            message = args[0]
        if len(args) > 1:
            data = args[1]
        if 'message' in kwargs:
            message = kwargs['message']
        kwargs.pop('message')
        if 'data' in kwargs:
            data = kwargs['data']
            kwargs.pop('data')

        kwargs['content'] = json.dumps(dict(message=message, data=data))

        if 'content_type' not in kwargs:
            kwargs['content_type'] = 'application/json'

        super(JsonDataResponse, self).__init__(**kwargs)

Use the class defined above in the return of a view:

from place.of.your.definition import JsonResponse

def example_view(request):
    number_of_foo = Foo.objects.filter(...)
    ...
    return JsonResponse({'number_of_foo': number_of_foo})
thoreg
  • 11
  • 3