unity&C#でのJsonの簡単な使用法(LitJson.dllを使用)



Simple Usage Json Using Litjson



最近、ゲームネットワーク通信を行う場合、多くの情報を送信する必要があります。以前は、あらゆる種類の情報を文字列につなぎ合わせてから、情報を取得するために文字列を受信して​​分割していましたが、情報が多いほど面倒です。転送する次のjsonを学習するだけで、キーと値のペアを使用して情報を設定し、情報を解析する方がはるかに便利です。 LitJsonは、パッケージ化されたC#Json処理dllであり、わずか数十Kです。

1.まず、LitJson.dllプラグインをダウンロードします。

より具体的な紹介、たくさんのオンライン検索を紹介する方法



ダウンロードリンク: https://download.csdn.net/download/liu1067082341/10717875

他の場所に行ってたくさんのオンラインをダウンロードすることもできます



2.基本的な使用法

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.操作の結果