在开始介绍json解析之前,我先给大家推荐一款插件,Json2Csharp
顾名思义,这是一款将json格式的字符串转化为c#类的插件,使用这个插件或多或少对我们提高写代码的速度可以起到一定帮助的。至于好不好用,这点就得看个人习惯了。
在这里我们使用Net中的json库,首先创建一个c#项目,然后添加引用到工程中来:
Newtonsoft.Json这个就是我们接下来要使用的。
首先呢,我们先看下我们要解析的json文件格式:
{"frames": [
{
"filename": "Frame0.png",
"frame": {"x":1,"y":1,"w":300,"h":300},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":300,"h":300},
"sourceSize": {"w":300,"h":300},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "Frame1.png",
"frame": {"x":303,"y":1,"w":300,"h":300},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":300,"h":300},
"sourceSize": {"w":300,"h":300},
"pivot": {"x":0.5,"y":0.5}
}],
"meta": {
"app": "http://www.codeandweb.com/texturepacker",
"version": "1.0",
"image": "555.png",
"format": "RGBA8888",
"size": {"w":1812,"h":302},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:0f3b7d122cb970503812013061d16802:0b912c8841fe53909c719b577b56c997:d629ea27b5d83b669af765908caf603e$"
}
}
接下来,我们根据上面的格式生成对应的c#类,这里我直接上代码了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Utility.json
{
public class JsonHelpers
{
[JsonProperty("frames")]
public Frames[] Frames { get; set; }
[JsonProperty("meta")]
public Meta Meta { get; set; }
}
public class Frames
{
[JsonProperty("filename")]
public string Filename { get; set; }
[JsonProperty("frame")]
public Frame Frame { get; set; }
[JsonProperty("rotated")]
public bool Rotated { get; set; }
[JsonProperty("trimmed")]
public bool Trimmed { get; set; }
[JsonProperty("spriteSourceSize")]
public SpriteSourceSize SpriteSourceSize { get; set; }
[JsonProperty("sourceSize")]
public SourceSize SourceSize { get; set; }
[JsonProperty("pivot")]
public Pivot Pivot { get; set; }
}
public class Frame
{
[JsonProperty("x")]
public int? X { get; set; }
[JsonProperty("y")]
public int? Y { get; set; }
[JsonProperty("w")]
public int? W { get; set; }
[JsonProperty("h")]
public int? H { get; set; }
}
public class Pivot
{
[JsonProperty("x")]
public double? X { get; set; }
[JsonProperty("y")]
public double? Y { get; set; }
}
public class SourceSize
{
[JsonProperty("w")]
public int? W { get; set; }
[JsonProperty("h")]
public int? H { get; set; }
}
public class SpriteSourceSize
{
[JsonProperty("x")]
public int? X { get; set; }
[JsonProperty("y")]
public int? Y { get; set; }
[JsonProperty("w")]
public int? W { get; set; }
[JsonProperty("h")]
public int? H { get; set; }
}
public class Size
{
[JsonProperty("w")]
public int? W { get; set; }
[JsonProperty("h")]
public int? H { get; set; }
}
public class Meta
{
[JsonProperty("app")]
public string App { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("image")]
public string Image { get; set; }
[JsonProperty("format")]
public string Format { get; set; }
[JsonProperty("size")]
public Size Size { get; set; }
[JsonProperty("scale")]
public double? Scale { get; set; }
[JsonProperty("smartupdate")]
public string Smartupdate { get; set; }
}
}
然后我们使用DeserializeObject将json文件读取到我们的类中来:
JsonHelpers json =Newtonsoft.Json.JsonConvert.DeserializeObject<JsonHelpers>(File.ReadAllText("D:\\1111.json"));//反序列化
在这里我只简单的输出以下meta类的信息来验证一下解析结果:
app:http://www.codeandweb.com/texturepacker
version:1.0
image:555.png
format:RGBA8888
size:JsonConvert.Size
scale:1
smartupdate:$TexturePacker:SmartUpdate:0f3b7d122cb970503812013061d16802:0b912c8841fe53909c719b577b56c997:d629ea27b5d83b669af765908caf603e$
这是输出结果,当然了size:JsonConvert.Size这里的输出是有问题的,那是因为我把我们自定义的类直接输出了。
public override string ToString()
{
string tmp = "" ;
tmp += "app:" + App + "\n";
tmp += "version:" + Version + "\n";
tmp += "image:" + Image + "\n";
tmp += "format:" + Format + "\n";
tmp += "size:" + Size + "\n";
tmp += "scale:" + Scale.ToString() + "\n";
tmp += "smartupdate:" + Smartupdate + "\n";
return tmp;
}
只要重载以下Size类的ToString方法,按照期望的格式输出就好了。。