目前我用得较多的是Newtonsoft ,后面了解到还有轻量的MiniJSON,SimpleJson以及litjson。
这里MiniJSON最精简,是一个class文件,通过对string进行简单字符串的处理来解析。
然后就是SimpleJson,也是一个class文件,不过代码量超级多,功能相对比较全面。看其他博客对这个评价蛮高的,不过我没使用过,暂不评价。
litjson,就文件多一点,好几个class组成。但是看部分博客提到跨平台上有问题。
由于之前是做windows应用,所以一直使用的是Newtonsoft,代码量远超上面的几个,编译出来的dll也是比他们的要大。所以,比较推荐用Newtonsoft,可定制化高很多,功能和内部考虑的东西也完善。
本文暂不讲怎么使用,只简单介绍怎么去github获取上面几个的源码,以及怎么编译。
提到github,这是一个好东西,好多好的开源项目都在上面。不过国内网速超级慢,使用起来不是很方便。一般情况下,可以不注册账号直接下载项目代码,下载下来是一个zip文件。也可以用VS里面的扩展插件GitHub Extension for Visual Studio,下载安装过程有点久,请耐心等待:
使用起来相对没svn好用(当然,这里是可以用svn来使用的)。
推荐使用客户端:GitHubDesktop :https://desktop.github.com/ 下载下来的应该是一个77M的全量包,直接安装就可以使用:
这里使用到的就是File-->Clone...;打开后
这里的url,就是网页上获取的:
这里贴一下看到的其他博主写的专门介绍GitHub Desktop的:https://www.cnblogs.com/hanford/p/6038417.html
一.MiniJSON
github地址: https://gist.github.com/darktable/1411710
直接在你的项目中新建一个class,全选代码后复制过去,就可以使用了。
由于需要vpn,所以这里放一下代码吧:
/*
* 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace MiniJSON {
// Example usage:
//
// using UnityEngine;
// using System.Collections;
// using System.Collections.Generic;
// using MiniJSON;
//
// public class MiniJSONTest : MonoBehaviour {
// void Start () {
// var jsonString = "{ \"array\": [1.44,2,3], " +
// "\"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 }";
//
// var dict = Json.Deserialize(jsonString) as Dictionary<string,object>;
//
// Debug.Log("deserialized: " + dict.GetType());
// Debug.Log("dict['array'][0]: " + ((List<object>) dict["array"])[0]);
// Debug.Log("dict['string']: " + (string) dict["string"]);
// Debug.Log("dict['float']: " + (double) dict["float"]); // floats come out as doubles
// Debug.Log("dict['int']: " + (long) dict["int"]); // ints come out as longs
// Debug.Log("dict['unicode']: " + (string) dict["unicode"]);
//
// var str = Json.Serialize(dict);
//
// Debug.Log("serialized: " + str);
// }
// }
/// <summary>
/// This class encodes and decodes JSON strings.
/// Spec. details, see http://www.json.org/
///
/// JSON uses Arrays and Objects. These correspond here to the datatypes IList and IDictionary.
/// All numbers are parsed to doubles.
/// </summary>
public static class Json {
/// <summary>
/// Parses the stri