How to make json api

Sorry if this is the main question. But I plan to make api for my site. At first, this api will be used by me, but later I will release its use for developers. I am confused, this site will be used for both mobile applications and websites. I plan to make an Android app for this.

The question is, should I make this api in different languages, i.e. in Java for Android forms, in Objective-C for iOS and javascript and php for the Internet?

Will it work for multiple platforms if I only do this in javascript?

+7
source share
4 answers

On the server side, your JSON API will be written in one language on one platform, it can be PHP, .NET or any platform of your choice.

On the client side (iPhone, Android, etc.) you will need to write a client capable of making requests and processing responses to your JSON API.

However, to ensure consistency in your client API, you can use a template, such as a request-response template, I always use it as easy to use and for implementation.

The idea is every JSON API method, you have a Request class and a Response class. You should also write a service client that represents your JSON API.

Example

Let's say I have a JSON service that gives me the contact information from my address book, it can have the following service methods:

/contact/{id} /address_book /save_contact/{id} 

My service client (example in Java) can have this interface:

 public interface AddressBookClient { public GetContactResponse getContact(GetContactRequest request); public GetAddressBookResponse getAddressBook(GetAddressBookRequest request); public SaveContactResponse saveContact(SaveContactRequest request); } 

Although the implementation will differ on client platforms, using the same approach or template will maintain their consistency.

Hope this helps.

+7
source

I would suggest something like a full API REST API.

This makes your API language independent.

After creating the API, you can create client libraries in popular languages.

+6
source

If the API is based on JSON, it is implemented for almost any popular language today. You can communicate between applications in different languages ​​without any problems.

You can see the extensive list of libraries for different languages ​​on the official JSON website .

+3
source

I use the url for the JSON request:

 api.example.com/api.php?action=User.login&username=admin&password=111 

JSON answer:

 { "resultcode":100, "resultdata":{ "id":"1", "username":"admin", "password":"$1$$TUPijxhaACBBZHMowbtgD.", "name":"chenwendong", "email":" mdowen@foxmail.com ", "sex":"\u7537", "age":"11", "phone":"15811111111", "address":"\u5317\u4eac\u7231\u7231\u7231\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a", "province_id":"1", "city_id":"1", "last_login_ip":"127.0.0.1", "last_login_time":"2012-05-07 17:32:55", "state":"1", "intime":"2012-05-07 09:49:11", "is_deleted":"0", "province":"\u5317\u4eac\u5e02", "city":"", "sid":"fcd6321c4cd4748a0cee2e7cc337506b", "uid":"1", "message_count":"11" } } u4eac \ u7231 \ u7231 \ u7231 \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ u554a \ { "resultcode":100, "resultdata":{ "id":"1", "username":"admin", "password":"$1$$TUPijxhaACBBZHMowbtgD.", "name":"chenwendong", "email":" mdowen@foxmail.com ", "sex":"\u7537", "age":"11", "phone":"15811111111", "address":"\u5317\u4eac\u7231\u7231\u7231\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a", "province_id":"1", "city_id":"1", "last_login_ip":"127.0.0.1", "last_login_time":"2012-05-07 17:32:55", "state":"1", "intime":"2012-05-07 09:49:11", "is_deleted":"0", "province":"\u5317\u4eac\u5e02", "city":"", "sid":"fcd6321c4cd4748a0cee2e7cc337506b", "uid":"1", "message_count":"11" } } 

Then someone can use Android JSON to decode it.

+1
source

All Articles