APIJSON/README.md
2016-12-06 21:28:01 +08:00

19 KiB

APIJSON

查看中文文档

A JSON Transmission Structure Protocal.

You can set any JSON structure and request your server, and the server will return a JSON String with the structure you had set like this:

Request:

{
    "[]": {                           //request an array
        "page": 1,                //array condition
        "count": 2,        
        "User": {                  //request an object from the table named User
            "sex": 0               //object condition
        },
        "Work": {
            "userId": “/User/id”  //rely path with default parent path,starts from the same level object's path
        },
        "Comment[]": {            //request an array named Comment
            "page": 0,
            "count": 3,
            "Comment": {
                 "workId": “[]/Work/id”  //full rely path
             }
        }
    }
}

Response:

{
    "[]":{
        "0":{
            "User":{
                "picture":"",
                "id":"38710",
                "sex":"0",
                "phone":"1300038710",
                "name":"Name-38710",
                "head":"http://www.tooopen.com/view/38710.html"
            },
            "Work":{
                "id":470,
                "title":"Title-470",
                "content":"This is a Content...-470",
                "userId":38710,
                "picture":"http://www.tooopen.com/view/470.html"
            },
            "Comment[]":{
                "0":{
                    "Comment":{
                        "id":4,
                        "parentId":0,
                        "workId":470,
                        "userId":310,
                        "targetUserId":14604,
                        "content":"This is a Content...-4",
                        "targetUserName":"targetUserName-14604",
                        "userName":"userName-93781"
                    }
                },
                "1":{
                    "Comment":{
                        "id":22,
                        "parentId":221,
                        "workId":470,
                        "userId":332,
                        "targetUserId":5904,
                        "content":"This is a Content...-22",
                        "targetUserName":"targetUserName-5904",
                        "userName":"userName-11679"
                    }
                },
                "2":{
                    "Comment":{
                        "id":47,
                        "parentId":4,
                        "workId":470,
                        "userId":10,
                        "targetUserId":5477,
                        "content":"This is a Content...-47",
                        "targetUserName":"targetUserName-5477",
                        "userName":"userName-80271"
                    }
                }
            }
        },
        "1":{
            "User":{
                "picture":"",
                "id":"70793",
                "sex":"0",
                "phone":"1300070793",
                "name":"Name-70793",
                "head":"http://www.tooopen.com/view/70793.html"
            },
            "Work":{
                "id":170,
                "title":"Title-73",
                "content":"This is a Content...-73",
                "userId":70793,
                "picture":"http://www.tooopen.com/view/73.html"
            },
            "Comment[]":{
                "0":{
                    "Comment":{
                        "id":44,
                        "parentId":0,
                        "workId":170,
                        "userId":7073,
                        "targetUserId":6378,
                        "content":"This is a Content...-44",
                        "targetUserName":"targetUserName-6378",
                        "userName":"userName-88645"
                    }
                },
                "1":{
                    "Comment":{
                        "id":54,
                        "parentId":0,
                        "workId":170,
                        "userId":3,
                        "targetUserId":62122,
                        "content":"This is a Content...-54",
                        "targetUserName":"targetUserName-62122",
                        "userName":"userName-82381"
                    }
                },
                "2":{
                    "Comment":{
                        "id":99,
                        "parentId":44,
                        "workId":170,
                        "userId":793,
                        "targetUserId":7166,
                        "content":"This is a Content...-99",
                        "targetUserName":"targetUserName-7166",
                        "userName":"userName-22949"
                    }
                }
            }
        }
    }
}

Compare with Previous HTTP Transmission Way

Process Previous way APIJSON
Transmission Server developers edit interfaces and update docs, then client developers request server and parse server responses according to the docs Client developers request server and parse server responses for their requirements
Compatibility Server developers add new interfaces tagged with v2 and update docs Nothing need to do
Client request Previous way APIJSON
Requirement Client developers append key-value pairs to the urls for requests in docs Client developers append JSON for their requirements
Structure base_url/lowercase_table_name?key0=value0&key1=value1...
&currentUserId=100&currentUserPassword=1234
the currentUserId and currentUserPassword is only for parts of interfaces
base_url/{TableName0:{key0:value0, key1:value1 ...}, TableName1:{...}...
, currentUserId:100, currentUserPassword:1234}
the currentUserId and currentUserPassword is only for parts of interfaces
URL Different urls for different method(GET,POST...) or requirements One url for one method(GET,POST...)
Key-Value Pair key=value key:value
Server operation Previous way APIJSON
Parse and response Get key-value pairs and query tables with them by the default way, and return JSON to client Just return what RequestParser#parse returned
Way of setting JSON structure to return Designed in server and cannot be modified by any client apps Designed by client apps and cannot be modified by sever
Client parse Previous way APIJSON
View Search docs or view logs after responses for requests Just view the requests, and viewing logs after responses for requests is also supported
Operate Parse JSON String from responses Parse with JSONResponse or use previous way
Client requests for different requirements Previous way APIJSON
User http://localhost:8080/user?id=1 http://localhost:8080/{"User":{"id":1}}
User and his Work Request twice
User: http://localhost:8080/user?id=1
Work: http://localhost:8080/work?userId=1
http://localhost:8080/{"User":{"id":1}, "Work":{"userId":"User/id"}}
User list http://localhost:8080/user/list?page=1&count=5&sex=0 http://localhost:8080/{"[]":{"page":1, "count":5, "User":{"sex":0}}}
Work list of which type is 1, each Work contains the publisher User and top 3 Comments The Work must contains the User Object and Comment Array
http://localhost:8080/work/list?page=1&count=5&type=1&commentCount=3
http://localhost:8080/{"[]":{"page":1, "count":5, "Work":{"type":1}, "User":{"workId":"/Work/id"}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}
Work list of an User, each Work contains the publisher User and top 3 Comments Change type=1 to userId=1 above ①Change "Work":{"type":1}, "User":{"workId":"/Work/id"} to "User":{"id":1}, "Work":{"userId":"/User/id"} above
②Or save 4 repeated User by this way
http://localhost:8080/{"User":{"id":1}, "[]":{"page":1, "count":5, "Work":{"userId":"User/id"}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}
③If the User is already obtained, you can also save all repeated User by this way
http://localhost:8080/{"[]":{"page":1, "count":5, "Work":{"userId":1}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}
Server responses for different requests Previous way APIJSON
User {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}} {"status":200, "message":"success", "User":{"id":1, "name":"xxx"...}}
User and his Work Reponse twice
User: {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}}
Work: {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}}
{"status":200, "message":"success", "User":{"id":1, "name":"xxx"...}, "Work":{"id":1, "content":"xxx"...}}
User list {"status":200, "message":"success", "data":[{"id":1, "name":"xxx"...}, {"id":2...}...]} {"status":200, "message":"success", "[]":{"0":{"User":{"id":1, "name":"xxx"...}}, "1":{"User":{"id":2...}}...}}
Work list of which type is 1, each Work contains publisher User and top 3 Comments {"status":200, "message":"success", "data":[{"id":1, "content":"xxx"..., "User":{...}, "Comment":[...]}, {"id":2...}...]} {"status":200, "message":"success", "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "User":{...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}
Work list of an User, each Work contains publisher User and top 3 Comments {"status":200, "message":"success", "data":[{"id":1, "content":"xxx"..., "User":{...}, "Comment":[...]}, {"id":2...}...]} ①{"status":200, "message":"success", "[]":{"0":{"User":{"id":1, "name":"xxx"...}, "Work":{...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}
②{"status":200, "message":"success", "User":{...}, "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}
③{"status":200, "message":"success", "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}

Usage

1.Download and unzip APIJSON project

Clone or download > Download ZIP > Unzip to a path and remember it.

2.Import MySQL table files

Start MySQLWorkbench > Enter a connection > Click Server menu > Data Import > Select the path

of APIJSON-Master/table > Start Import > Refresh SCHEMAS, and you'll see the tables were already added.

3.Run Server project with IntellIJ IDEA Ultimate

If you haven't installed it, please download and install before run.

4.Run Client project with ADT or Android Studio

If you haven't installed any editor above, please download and install one before run.

5.Operate Client app

Select an APIJSON request to send to server and wait. It will show the result received.

Download Client App

APIJSONClientApp.apk

If you have any questions or suggestions about APIJSON, you can send me an e-mail to tommylemon@qq.com.

Welcome star, welcome fork

https://github.com/TommyLemon/APIJSON

APIJSON, let interfaces go to hell !