My interface is like this. when there is a returned value, the data in the json will have a value, or it will be an empty string that the colleague told me that when he received the data, data is converted to an object in json format for retrieval. if the data is empty, the entire program will pop up, or the data will return null and cannot be returned...
My interface is like this. when there is a return, the data in the json will have a value, otherwise it will be an empty string.
My colleague told me that when he received the message, the data was converted to an object in json format for retrieval. if the data was empty, the entire program would crash, or I would return null for data, cannot return null. I don't understand. Is that true?
Reply content:
My interface is like this. when there is a return, the data in the json will have a value, otherwise it will be an empty string.
My colleague told me that when he received the message, the data was converted to an object in json format for retrieval. if the data was empty, the entire program would crash, or I would return null for data, cannot return null. I don't understand. Is that true?
It is true that your original data is an object, and it becomes an array and will be suspended if it is not processed. you can$ Info ['data'] = new stdClass (); // return an empty object
As a server, I think the field type should be sensitive.
Don't you need to judge the code value?
Does your colleague use the framework to parse the json file, so that he can manually parse it again. Do not use frameworks.
Otherwise, the framework cannot determine that [] in data is an empty object.
Of course, you can also determine the code value before parsing.
This is true if the client code is not well written. This pot has been dumped on you. if you are better than him, you will be dumped back. Otherwise, pick up the pot...
Generally, clients use a framework similar to gson to parse the json data returned by the server. the data format must be defined in advance. Since data is an object, you can use the default value null of the object. I think this requirement is quite normal.
I think this is the question of who can change it ~~
I think what you said makes sense too. Generally, my colleague can give me a convenient response. Then you are processing it yourself.
Of course, let's analyze the specific problem ~~
I think the code value is better.
As an API, the same field should return the same data type.
However, if the problem is solved, the code can also be used.
Therefore, if you change the interface during development, if it is already online, you should change the interface.
Therefore, joint interface debugging is required.
What your colleague said is no problem. make sure that the data types of each field are consistent. after all, are they strong types? haha
The write on the app side is not rigorous. first, judge the status code. if the status code is successful, the data will be parsed. if the data fails, the error code will be returned and no parsing will be performed,
Because of the weak php type on the server side, the app has requirements for each data type, that is, the strong type. the consistency of the returned data type should be ensured as much as possible. when the data is not empty, the returned json string is returned, when the app is parsed as an object and data is null, the server determines whether it is null. if it is null, a null object is assigned.
$obj = new stdClass();
Why didn't the php end define variables first ?? $ Data = array ()
In addition, the client should ensure that the client does not flash back, I suggest you simulate some error return values during the development process to the client to see if the client will flash back, and then let him back. haha, just kidding
$result = array( "code" => "2", "msg" => "", "data" => (Object)array());
Do not write directly
$result = array( "code" => "2", "msg" => "", "data" => array());