Unity3d使用json能够借助LitJson
下载LitJson,复制到Unity3d工作文件夹下
于是能够在代码中实现了
以下发送请求到server并解析
System.Collections.Hashtable headers = new System.Collections.Hashtable ();
headers.Add ("Content-Type","application/x-www-form-urlencoded");
string data = "token="+User.token;
byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes (data);
WWW www = new WWW ("http://127.0.0.1:8080/userver/st",bs,headers);
yield return www;
if(www.error!=null){
m_info=www.error;
yield return null;
}
JsonData listJson = JsonMapper.ToObject (www.text);
//server返回map数组
Debug.Log (listJson[0][0]);
//json对象的值能够通过下标或者key获取
Debug.Log (listJson[0]["r"]);
以下是server数据
Map map=new HashMap();
map.put("r","1212121212d12");
map.put("e","1212121212a12");
map.put("a","1212121212d12");
map.put("f","1212121212c12");
map.put("d","12121212121g2");
map.put("s","sdfdsfds");