
public void WriteJson(object ob, string jsonName)
{
string json = LitJson.JsonMapper.ToJson(ob)
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json)
string filePath = Application.dataPath + "/Resources/" + jsonName + ".json"
using (System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
stream.Write(bytes, 0, bytes.Length)
}
UnityEditor.AssetDatabase.Refresh()//属性掘薯困Asset,生成完成后立刻可以看到文件
}
方法二 jsonwriter 半编译写法
void CreateJson()
{
string path = Application.dataPath + "/Resources/Jsons/" + objBase.transform.name + ".json"
FileInfo fileInfo = new FileInfo(path)
StreamWriter sw = fileInfo.CreateText()
StringBuilder sb = new StringBuilder()
JsonWriter jsonwriter = new JsonWriter(sb)
jsonwriter.WriteObjectStart()//1
for (int i = 0i <numbers.Counti++)
{
jsonwriter.WritePropertyName("Number" + (i + 1))
jsonwriter.WriteObjectStart()//1-1
type = new List<Transform>()
foreach (Transform child in numbers[i].transform)
type.Add(child)
jsonwriter.WritePropertyName("TypeCount")jsonwriter.Write(type.Count)
手举 for (int j = 0j <type.Countj++)
{
jsonwriter.WritePropertyName("Type"判念 + (j + 1))
jsonwriter.WriteObjectStart()//1-1-1
pos = new List<Transform>()
foreach (Transform child in type[j].transform)
pos.Add(child)
jsonwriter.WritePropertyName("Size")jsonwriter.Write(pos[0].transform.localScale.x.ToString())
jsonwriter.WritePropertyName("Pos")
jsonwriter.WriteArrayStart()
for (int k = 0k <pos.Countk++)
{
jsonwriter.Write("(" + pos[k].transform.localPosition.x + "," + pos[k].transform.localPosition.y + ")")
}
jsonwriter.WriteArrayEnd()
jsonwriter.WriteObjectEnd()//1-1-1
}
jsonwriter.WriteObjectEnd()//1-1
}
jsonwriter.WriteObjectEnd()//1
sw.WriteLine(sb.ToString())
sw.Close()
AssetDatabase.Refresh()
}
Json 里面的数据是double 类型,使用float 会出问题
double.Parse float .Parse 不一样
好的,boost写json去掉引号是指使用boost库来编拿锋竖写json文件,并且去掉引号。要完成这个任务,首先需要安装boost库,然后使用boost库的json类来编写json文件,并且在编写的过程中去掉引号。安装boost库的方法是,首先从官网下载boost库,然后解压缩,把解压后的文件夹放到指定的位置,然后在命令行中运行bootstrap.bat文件,最后运行b2.exe文件,这样就可以安装boost库了。
使用boost库的json类来编写json文件,首先需要消大引入boost库的头文件,然后创建一个json对象,然后使用json对象的add_member函数来添加json元素,最后使用json对象的write函数来写入json文件,这样就可以编写json文件了。
在基祥编写的过程中去掉引号,可以使用boost库的string_ref类来实现,首先创建一个string_ref对象,然后使用string_ref对象的remove_quotes函数来去掉引号,最后使用string_ref对象的to_string函数来获取去掉引号后的字符串,这样就可以去掉引号了。
先生成map 然后备弯用alibaba的fastjson,附件中的这个包,里头有你意想不到的API例如:
Map map = ......
map的结构和要生成的字符串结构一致就行,你的optionset在map里头的结构就是一个list
list optionset= new ArrayList()
Map temp = new HashMap()temp.put('set',xxx)temp.put('value',xxx)
list.add(temp)
....有多少个就添加多少,
然后把list添加到最外层的Map对象中燃亏;
map.put('optionset',list)key就是皮滚神json字符中对应的key
拼装好了map然后就是下一句就搞定了
String josnStr = JSONObject.toJSONString(map)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)