简体   繁体   中英

How to make json api

Sorry if this is a basic question. But I am planning to make an api for my website. First this api will be used by me but later I will release for the developers to use it. I am confused, this website is going to be used for both mobile apps and for websites. I am planning to make android app for this.

The question is do I have to make this api in different languages ie in Java for android plateforms, in Objective C for iOS and javascript and php for web?

Will it work for several platforms if I only make this in javascript?

On the server side, your JSON API would be written in one language on one platform, this could be PHP, .NET or any platform of your choice.

On the client side (iPhone, Android, etc) You would need to write a client that is capable of making requests and handling responses to your JSON API.

However, in order to enforce consistency across your client API's you can employ a pattern, such as the request-reply pattern, I use this all the time as its easy to use and to implement.

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

An example

Let's say I have a JSON service that gives me contact details from my address book, it could have these service methods:

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

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

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

Although the implementation would be different across client platforms, using the same approach or pattern would keep them consistent.

Hope that helps.

I would suggest something like a JSON based REST full API.

This makes your API language independent.

Once the API is made then you can make client libraries in popular languages.

If the API is JSON based, it is implemented for pretty much any popular language today. You can communicate between applications build in different languages with no issues.

You can see an extensive list of libraries for different languages at the official JSON site .

I'm using a URL to request the JSON:

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

The JSON response is:

{
   "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM