0

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

StefMa
  • 3,344
  • 4
  • 27
  • 48

4 Answers4

3

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.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
1

I am not sure with JSON but there alreday was a thread explaining JSON (google knows everything). Maybe you can find some help here:

What is JSON and why would I use it?

http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/

EDIT: I forgot to answer the question why not to use your own function. Of course you can use it and it works. But a lot of services give a JSON to you. It is like a standard. Furthermore there is an JavaClass. So you do not have to do the work which others already have done (see: http://goo.gl/9X4HU)

Best regards

Community
  • 1
  • 1
jtight_404
  • 51
  • 5
  • It's only that, maybe if i want, other or 3-party developers can easilie work with that too?! – StefMa Apr 26 '13 at 06:38
  • 1
    I hope, that I got the question right. I see no problem to use your own format. But if you want to work with third-party developers then you have to give them a framework that they can easily work with that. If you use JSON than there will be absolutely no problem with third-party developers. – jtight_404 Apr 26 '13 at 07:14
1

Don't do it by hand, it's error-prione and violates DRY (don't repeat yourself). Instead:

  1. On server use a REST framework that automatically produces JSON. For example RESTEasy. Search the net for examples.

  2. On Android use either built in support for JSON or better use on of well-known and tested libs: GSON or Jackson. See some speed comparisons. Alternativelly you can use Spring Android, which mashes networking+JSON in one easy to use package.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

I use JSON in Android because it is lightweight data format which I can easily convert to Java objects using this google library.

You always have 2 possibilities - to use some library, or to write the code by yourself. I'm not saying that using the library is always an option, but in many cases it can save your time and reduce errors. It's up to you to decide.