Search code examples
javaandroidjsongoogle-app-enginegoogle-cloud-datastore

Explain the functionality of JSON


I think it is better to understand why I have so many problems with JSON that I explain you what my goal is:

I work with Googles App Engine. There I want to store data. The data looks like

user - username
question - question
date1 - date1
date2 - date2

An Android App have the "simple" function to: Send the data which the user has entered and reviece the data from the complete database.

Ok, fine. So I searched for a good "API" for that. The question about that was: "how can I read the data" and "how can I sent". The "simple" anwere was: use JSON.. . Many people say's that to me.

The first step was to show the data from the database. I write in python that:

json.dumps({"info": [{'user': 'username1', 'question': 'question1', 'date1':'date1', 'date2':'date1'}, {'user': 'username2', 'question': 'question2', 'date1':'date2', 'date2':'date2'}]})

It works. On the Client site I write in Java these:

        JSONObject ob = new JSONObject(result);
        JSONArray arNames = ob.getJSONArray("info");

        for(int i = 0; i < arNames.length(); i++){
            JSONObject c = arNames.getJSONObject(i);
            Log.i("name", c.getString("name"));
            Log.i("frage", c.getString("question"));
        }

These works also.

But (and now the main question about the thread!): Why we use JSON to format?! Why? I can with this data an other simple "API" without the JSON libarys and classes.

Example: If I say on the Server site only:

!user:user;question:question;date1:date1;date2:date2
!user:user1;question:question1;data2:date3;date2:date3
... and so one...

On the Client site the same:

[READ THE DATA WITH ClientHTTP]
String[] all = result.Split("!");
for(int i = 0; i<all.length; i+= 1)
{
   String[] split2 = all[i].Split(";");
   String[] user = split2[0].Split(":");
   // user[1] holds now the user
   String[] split3 = split2[1].Split(";");
   String[] questinn = split3.Split(":");
   // question[1] holds now the question
   ... AND SO ONE!

So, why I use JSON? My option or example do the same. But with my own Syntax..

Thank you for help


Solution

  • JSON is a standard format and it's implementations make it easy to use -- No split() and other stuff necessary. Also, it's supported by all kinds of programming languages (like Python and Java in your own example) and so it provides a simple way to exchange data between completly different systems. And it's well thought out and could for example also handle questions with ':' or ';' in it. A case where your suggested solution would fail.