Ús senzill de Json (amb LitJson.dll) en unitat i C #
Simple Usage Json Using Litjson
Recentment, quan es fa comunicació de xarxa de jocs, és necessari transmetre molta informació. Abans, tota mena d’informació s’empalma en una cadena i, després, es rep i divideix la cadena per obtenir informació, però quan hi ha més informació és més problemàtica. Només aprendre el següent json a transferir, és molt més convenient establir informació i analitzar informació mitjançant parells valor-clau. LitJson és un dll de processament C # Json empaquetat, que és només unes poques desenes de K.
1. Primer, descarregueu el connector LitJson.dll.
Com introduir una introducció més específica, molta cerca en línia
enllaç de descàrrega: https://download.csdn.net/download/liu1067082341/10717875
També podeu anar a altres llocs per descarregar-vos molta xarxa
2. Ús bàsic
static void Main(string[] args) { / / The simplest usage (taking student information as an example) JsonData student_set = new JsonData() //Create a json object and add a key-value pair student_set['name'] = 'ZhangSan' student_set['age'] = 18 String strJson = student_set.ToJson()//convert json to a string Console.WriteLine('The converted string is:' + strJson) JsonData student_get = JsonMapper.ToObject(strJson)//Convert the string to a json object Console.WriteLine(student_get['name'].ToString()) // Output the value of the key in the json object to 'name' Console.WriteLine('
') / / With the use of child objects, add a child object based on the above j info student_set['info'] = new JsonData() student_set['info']['phone'] = 123456 student_set['info']['address'] = 'anhui' strJson = student_set.ToJson() Console.WriteLine('The converted string is:' + strJson) JsonData student_get2 = JsonMapper.ToObject(strJson) Console.Write('phone number:'+student_get2['info']['phone'] + 'Address:' + student_get2['info'][1]) //Two methods output j sub-objects Information, key name or subscript Console.WriteLine('
') //Multiple sub-objects (in the case of players), each player is an object, each object has a name, and two key-value pairs JsonData player_set = new JsonData() player_set['player1'] = new JsonData() player_set['player1']['name'] = 'Yasuo' player_set['player1']['level'] = '10' player_set['player2'] = new JsonData() player_set['player2']['name'] = 'Lee' player_set['player2']['level'] = '20' player_set['player3'] = new JsonData() player_set['player3']['name'] = 'Zed' player_set['player3']['level'] = '30' strJson = player_set.ToJson() Console.WriteLine('The converted string is: '+ strJson) JsonData player_get = JsonMapper.ToObject(strJson) Console.WriteLine ('Common resolution to '+player_get.Count+' player information') / / can get a total of several objects, the number of sub-objects can also be obtained Console.WriteLine('Player One Name: ' + player_set['player1']['name'] + ' Player Level: ' + player_get[0][1]) Console.ReadKey() }
3. Resultats de l'operació