日期:2014-05-16  浏览次数:20484 次

Unity3d之MiniJson与LitJson之间的较量

由于项目不得不用到json来解析服务器端传来的数据,于是不得不选择一种在unity3d上面可用的json.开始根据网上推荐LitJson,于是下载下来源码,导入项目;

经过测试可以用;但是移植到ipad时,就各种问题了,上网查问题也没结果,就发现有人说JsonMapper is not compatible with IOS,太坑爹不兼容

于是又根据网友推荐的MiniJson,功能虽然没有litjson强大,不能够解析嵌套数组,但是可以满足自己现在的需求。最终经过两天的挣扎,测试litjson无法可用,选择了MiniJson. (测试期间还接触了jsonfx,但是导入项目时出错,就不折腾了)

MiniJson下载:

https://gist.github.com/darktable/1411710

MiniJson测试代码:

using MiniJson;

void Start () 
	{
		
	string jsonString = "{ \"array\": [1.44,22,33]" +
                        "\"object\": {\"key1\":\"value1\", \"key2\":256}, " +
                        "\"string\": \"The quick brown fox \\\"jumps\\\" over the lazy dog \", " +
                        "\"unicode\": \"\\u3041 Men\\u00fa sesi\\u00f3n\", " +
                        "\"int\": 65536, " +
                        "\"float\": 3.1415926, " +
                        "\"bool\": true, " +
                        "\"null\": null }";
		
	//Dictionary<string, object> dict = MiniJSON.LC_MiniJson.Deserialize(jsonString) as Dictionary.<string. Object>;
	Dictionary<string, object> dict = MiniJSON.LC_MiniJson.Deserialize(jsonString) as Dictionary<string, object>;
    Debug.Log("deserialized: " + dict.GetType());
		


		//double[][] arrayList  = MiniJSON.LC_MiniJson.Deserialize((dict["array"] as List<object>)[0].ToString()) as double[][];
		
		List<object> lst = (List<object>) dict["array"];
		
	Debug.Log("dict['array'][0]: "+ lst[2]);
	
    
    Debug.Log("dict['string']: " + dict["string"].ToString());
    Debug.Log("dict['float']: " + dict["float"]); // floats come out as doubles
    Debug.Log("dict['int']: " + dict["int"]); // ints come out as longs
    Debug.Log("dict['unicode']: " + dict["unicode"].ToString());
		
	Dictionary<string, object> dict2 = (dict["object"]) as Dictionary<string, object>;
		
	
    string str = MiniJSON.LC_MiniJson.Serialize(dict2);

    Debug.Log("serialized: " + str);
	}


/*
 * Copyright (c) 2013 Calvin Rien
 *
 * Based on the JSON parser by Patrick van Bergen
 * http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
 *
 * Simplified it so that it doesn't throw exceptions
 * and can be used in Unity iPhone with maximum code stripping.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE S