In this example, we do two things:
1. Read a JSON data from the PHP program on the server and display the content.
2. convert a Data Structure to JSON and send it to the server.
This is an example of data exchange.
First, write a simple PHP program to generate JSON data.
Named test. php.
Java code
- <? PHP
- $ Arr = array (
- 'Username' => 'foo ',
- 'Password' => 'bar'
- );
- Echo json_encode ($ ARR );
- ?>
<? PHP $ arr = array ('username' => 'foo', 'Password' => 'bar'); echo json_encode ($ ARR);?>
This program is to display the $ arr array into JSON format data. PhP5 and above support json_encode. If PhP4 requires an additional support program, you can go to json.org to find it.
The second PHP program is to convert the JSON data from unity3d post to an array. The name is test1.php, which is also very simple.
Java code
- <? PHP
- $ Jsonstring = $ _ post ["jsonstring"];
- $ Jsondata = json_decode (stripslashes ($ jsonstring), true );
- Echo $ jsondata ["password"];
- ?>
<? PHP $ jsonstring = $ _ post ["jsonstring"]; $ jsondata = json_decode (stripslashes ($ jsonstring), true); echo $ jsondata ["password"];?>
How to use it in unity? It's not hard. The following is the code. You can give whatever name you like. My name is jsontest. js.
Java code
- VaR jsonurl = "http: // localhost/JSON/test. php ";
- VaR jsonurl1 = "http: // localhost/JSON/test1.php ";
- Function start (){
- // Method for obtaining JSON data
- VaR getwww: www = new WWW (jsonurl );
- Yield getwww;
- VaR jsonobj1 = eval (getwww. data );
- Print (jsonobj1 ["username"]);
- // Method for submitting JSON data
- VaR mydata = new boo. Lang. Hash ();
- Mydata ["username"] = "hello ";
- Mydata ["password"] = "world ";
- // Convert data to a JSON string
- VaR jsonstring = tojson (mydata );
- VaR form = new wwwform ();
- Form. addfield ("jsonstring", jsonstring );
- VaR postwww: www = new WWW (jsonurl1, form );
- Yield postwww;
- Print (postwww. data );
- }
- /**
- * JSON Conversion
- */
- Static function tojson (OBJ ){
- If (OBJ = NULL) Return "null ";
- VaR Results = new array ();
- For (VAR property in OBJ ){
- Results. Push ("\" "+ property. Key +" \ ": \" "+ property. Value + "\"");
- }
- Return "{" + results. Join (",") + "}";
- }
VaR jsonurl = "http: // localhost/JSON/test. PHP "; var jsonurl1 =" http: // localhost/JSON/test1.php "; function start () {// method for obtaining JSON data var getwww: WWW = new WWW (jsonurl); yield getwww; var jsonobj1 = eval (getwww. data); print (jsonobj1 ["username"]); // Method for submitting JSON data var mydata = new boo. lang. hash (); mydata ["username"] = "hello"; mydata ["password"] = "world "; // convert data to JSON string var jsonstring = tojson (mydata); var form = new wwwform (); form. addfield ("jsonstring", jsonstring); var postwww: www = new WWW (jsonurl1, form); yield postwww; print (postwww. data);}/*** convert JSON */static function tojson (OBJ) {If (OBJ = NULL) Return "null"; var Results = new array (); for (VAR property in OBJ) {results. push ("\" "+ property. key + "\": \ "" + property. value + "\" ");} return" {"+ results. join (",") + "}";}
In start (), convert JSON into Boo directly using eval. lang. in the hash format, retrieve the username. If you look at the console, foo is displayed, that is, $ arr ['username'] in PHP. Next, submit a username and password, which are hello and world. Then, the PHP feedback is displayed, and the world is displayed on the console.
The tojson () function is a simple function written by me, that is, the boo. lang. the hash array is converted into a JSON string and can only process one-dimensional arrays. If you are interested, you can modify it to support multi-dimensional arrays.
In this way, I used to use JSON data transmission. It would be much easier if I was familiar with using JSON format, we recommend that you study this simple and convenient format to improve program efficiency.
Er, the discuz code mode cannot be used for forums in safari... The Code tag can only be edited in Firefox ...... In addition, the tojson function does not support multi-dimensional arrays because it does not know how to express undefined in ECMA Javascript in unity3d...
We still do not set the reply. We can see that some people hold personal places, some flowers hold a flower field, and some points hold a sub-field ...... =. = |
To pass Chinese characters normally in JSON, refer to this post:
Http://web3d.5d6d.com/thread-2217-1-1.html
Basically, you can transfer Chinese Characters in JSON.
Refer to this post for the JSON method in U3D:
Http://bbs.vrsh.cn/thread-2095-1-1.html
In this example, the problem is that, if the data you transmit carries Chinese characters to U3D
VaR jsonobj1 = eval (getwww. data );
This step will cause errors. The reason is that unity3d does not support \ U escape, while the json_encode function directly converts Chinese to \ u1234 when encode is used.
In fact, it is a good solution. Before echo, convert the escape string to Chinese. Unity3d is not a browser. You should directly read the binary data when calling WWW, so there is no error.
I copied the js_unescape function ...... Simple change
Java code
- <? PHP
- $ Arr = array (
- 'Username' => 'test ',
- 'Password' => 'bar'
- );
- Echo js_unescape (json_encode ($ ARR ));
- Function js_unescape ($ Str)
- {
- $ Ret = '';
- $ Len = strlen ($ Str );
- For ($ I = 0; $ I <$ Len; $ I ++)
- {
- If ($ STR [$ I] = '\' & $ STR [$ I + 1] = 'U ')
- {
- $ Val = hexdec (substr ($ STR, $ I + 2, 4 ));
- If ($ Val <0x7f) $ ret. = CHR ($ Val );
- Else if ($ Val <0x800) $ ret. = CHR (0xc0 | ($ Val> 6). CHR (0x80 | ($ Val & 0x3f ));
- Else $ ret. = CHR (0xe0 | ($ Val> 12 )). CHR (0x80 | ($ Val> 6) & 0x3f )). CHR (0x80 | ($ Val & 0x3f ));
- $ I + = 5;
- }
- Else $ ret. = $ STR [$ I];
- }
- Return $ ret;
- }
- ?>
<? PHP $ arr = array ('username' => 'test', 'Password' => 'bar'); echo js_unescape (json_encode ($ ARR )); function js_unescape ($ Str) {$ ret = ''; $ Len = strlen ($ Str); For ($ I = 0; $ I <$ Len; $ I ++) {if ($ STR [$ I] = '\' & $ STR [$ I + 1] = 'U ') {$ val = hexdec (substr ($ STR, $ I + 2, 4); If ($ Val <0x7f) $ ret. = CHR ($ Val); else if ($ Val <0x800) $ ret. = CHR (0xc0 | ($ Val> 6 )). CHR (0x80 | ($ Val & 0x3f); else $ ret. = CHR (0xe 0 | ($ Val> 12 )). CHR (0x80 | ($ Val> 6) & 0x3f )). CHR (0x80 | ($ Val & 0x3f); $ I + = 5;} else $ ret. = $ STR [$ I];} return $ ret;}?>
Java code
- VaR jsonurl = "http: // localhost/JSON/demo. php ";
- VaR show;
- VaR chineseskin: guiskin;
- Function start (){
- // Method for obtaining JSON data
- VaR getwww: www = new WWW (jsonurl );
- Yield getwww;
- Print (getwww. data );
- VaR jsonobj1 = eval (getwww. data );
- Print (jsonobj1 ["username"]);
- Show = jsonobj1 ["username"];
- }
- Function ongui (){
- Gui. Skin = chineseskin;
- Gui. Button (rect (10, 10,), show );
- }
VaR jsonurl = "http: // localhost/JSON/demo. PHP "; var show; var chineseskin: guiskin; function start () {// method for obtaining JSON data var getwww: www = new WWW (jsonurl); yield getwww; print (getwww. data); var jsonobj1 = eval (getwww. data); print (jsonobj1 ["username"]); show = jsonobj1 ["username"];} function ongui () {GUI. skin = chineseskin; GUI. button (rect (10, 10,), show );}
A font is required to display Chinese characters. In addition, note that the encoding of. php files should be set to UTF-8, And the encoding of U3D JS files should also be set to utf8.
=====
In addition, the JSON method linked above is also effective for the tree structure:
For example
Java code
- $ Arr = array (
- 'Username' => 'hao ',
- 'Password' => 'bar ',
- 'A' => array (
- "Name" => "AAA ",
- "Pass" => "BBB"
- )
- );
$ Arr = array ('username' => 'hao', 'Password' => 'bar', 'A' => array ("name" => "AAA ", "Pass" => "BBB "));
Displayed
Java code
- Print (jsonobj1 ["A"] ["name"]);
Print (jsonobj1 ["A"] ["name"]);
You will see AAA display, so the group says
For example, the database query result contains three records, each containing the name, gender, and age. Can this JSON be obtained and then returned to U3D?
Yes, you can. Forget XML. However, at present, the code uploaded from U3D to the Web does not implement a JSON tree structure, and only one layer can be passed. This can also be implemented through modification.