前提需引入newtonsofjson,引入方法可先在visual studio 2019





将newtonsoft.json.dll文件导入unity的plugins下。
json格式字符串转lua格式字符串,效果如下:
json字符串
{
"1": "asd",
"2": true,
"3": 1.5,
"4": 123,
"a": {
"b": "c",
"d": [1, 2, 3]
}
}lua字符串 (.lua文件一般直接return {}表 故会加上 "return " 如果不需要可以自行修改代码。)
return {
[1] = "asd",
[2] = true,
[3] = 1.5,
[4] = 123,
a = {
b = "c",
d = { 1, 2, 3 }
}
}使用方法:
string json = "{\"1\":\"asd\",\"2\":true,\"3\":1.5,\"4\":123,\"a\":{\"b\":\"c\",\"d\":[1,2,3]}}";
string lua = milk.json.milkjsonutility.json2lua(json, true);类说明:
luaobject:封装json基础类型对象,核心getstring方法返回对象的字符串形式。
luatable:封装jsonarray或jsonobject类型对象,主要是将数组或对象内的成员封装为luaobject存储起来,数组存储于list,对象成员存储于map,核心getstring方法返回表的字符串形式。
luatable类静态方法:public static luatable createfromjson(string json)
由该方法进行转化json为lua,使用jobject.parse(json)转为json对象,后经过判断是数组还是对象类型进行使用不同函数转化(jsonarray2luatable、jsonobject2luatable)
jsonarray2luatable:将json数组成员一一转化为luatable
jsonobject2luatable:将json对象成员一一转化为luatable
namespace milk.json
{
public static class milkjsonutility
{
/// <summary>
/// 将json格式字符串转成lua表形式字符串
/// </summary>
/// <param name="json"></param>
/// <param name="indented">是否缩进</param>
/// <returns></returns>
public static string json2lua(string json, bool indented = false)
{
var table = luatable.createfromjson(json);
return $"return {table.getstring(indented)}";
}
}
}using system;
using system.collections.generic;
using system.text;
namespace milk.json
{
public enum luavaluetype
{
table,
string,
num,
boolean,
nil
}
}using system;
using system.collections.generic;
using system.text;
using newtonsoft.json.linq;
namespace milk.json
{
public class luatable
{
//存放table中的有序部分
private list<luaobject> list = new list<luaobject>();
//存放table中的无序部分
private dictionary<string, luaobject> map = new dictionary<string, luaobject>();
//所在层级
private int layer = 0;
public luatable(int _layer)
{
layer = _layer + 1;
}
#region 有序数组添加
/// <summary>
/// [有序]添加string value
/// </summary>
/// <param name="value"></param>
public void additem(string value)
{
list.add(new luaobject(value));
}
/// <summary>
/// [有序]num value
/// </summary>
/// <param name="value"></param>
public void additem(int value)
{
list.add(new luaobject(value));
}
/// <summary>
/// [有序]num value
/// </summary>
/// <param name="value"></param>
public void additem(float value)
{
list.add(new luaobject(value));
}
/// <summary>
/// [有序]bool value
/// </summary>
/// <param name="value"></param>
public void additem(bool value)
{
list.add(new luaobject(value));
}
public void additem(luatable value)
{
list.add(new luaobject(value));
}
/// <summary>
/// 加入nil
/// </summary>
public void additemnil()
{
list.add(new luaobject());
}
#endregion
#region key_value添加
public void additem(string key, string value)
{
//查重
if (!map.containskey(key))
{
map.add(key, new luaobject(value));
}
}
public void additem(string key, int value)
{
//查重
if (!map.containskey(key))
{
map.add(key, new luaobject(value));
}
}
public void additem(string key, float value)
{
//查重
if (!map.containskey(key))
{
map.add(key, new luaobject(value));
}
}
public void additem(string key, bool value)
{
//查重
if (!map.containskey(key))
{
map.add(key, new luaobject(value));
}
}
public void additem(string key, luatable value)
{
//查重
if (!map.containskey(key))
{
map.add(key, new luaobject(value));
}
}
public void additemnil(string key)
{
//查重
if (!map.containskey(key))
{
map.add(key, new luaobject());
}
}
#endregion
private bool isnumeric(string str)
{
int number;
return int.tryparse(str, out number);
}
private string indentedlayer(string str, bool indented, int change = 0)
{
if (indented)
{
for (int i = 0; i < layer + change; i++)
{
str = "\t" + str;
}
}
return str;
}
public string getstring(bool indented = false)
{
string str = "{";
bool haslist = list.count > 0;
bool hasmap = map.count > 0;
bool isneedindented = false;
if (indented && (haslist || hasmap))
{
isneedindented = true;
if (haslist)
{
luavaluetype type = list[0].getluavaluetype();
if (type == luavaluetype.boolean || type == luavaluetype.num)
{
isneedindented = false;
}
}
if (isneedindented)
str += "\n";
}
if (haslist)
{
int count = 0;
foreach (var item in list)
{
count++;
if (!isneedindented)
{
if (indented && count == 1)
str += " ";
str += item.getstring(indented);
if (count < list.count)
str += ",";
if (indented)
str += " ";
}
else
{
str += indentedlayer(item.getstring(indented), indented);
if (count < list.count)
str += ",";
if (indented)
str += "\n";
}
}
}
if (hasmap)
{
int count = 0;
foreach (var item in map)
{
count++;
var key = item.key.replace('$', '_');
if (isnumeric(key))
{
key = $"[{key}]";
}
if (indented)
str += indentedlayer($"{key} = {item.value.getstring(indented)}", indented);
else
str += indentedlayer($"{key}={item.value.getstring(indented)}", indented);
if (count < map.count)
str += ",";
if (indented)
str += "\n";
}
}
if (!isneedindented)
{
str += "}";
}
else
{
str += indentedlayer("}", indented, -1);
}
return str;
}
public static luatable createfromjson(string json)
{
var jsonobject = jobject.parse(json);
if (jsonobject.type == jtokentype.array)
{
return jsonarray2luatable(jsonobject.toobject<jarray>(), 0);
}
else
{
return jsonobject2luatable(jsonobject, 0);
}
}
/// <summary>
/// 递归方法
/// </summary>
static luatable jsonobject2luatable(jobject jsonobj, int layer)
{
var curluatable = new luatable(layer);
//构建无序table(lua表 key-value形式)
foreach (var item in jsonobj)
{
switch (item.value.type)
{
case jtokentype.boolean:
curluatable.additem(item.key, (bool)item.value);
break;
case jtokentype.array:
curluatable.additem(item.key, jsonarray2luatable(item.value.toobject<jarray>(), layer + 1));
break;
case jtokentype.string:
curluatable.additem(item.key, (string)item.value);
break;
case jtokentype.date: //转成string
curluatable.additem(item.key, (string)item.value);
break;
case jtokentype.float:
curluatable.additem(item.key, (float)item.value);
break;
case jtokentype.integer:
curluatable.additem(item.key, (int)item.value);
break;
case jtokentype.none:
curluatable.additemnil(item.key);
break;
case jtokentype.null:
curluatable.additemnil(item.key);
break;
case jtokentype.object:
curluatable.additem(item.key, jsonobject2luatable(item.value.toobject<jobject>(), layer + 1));
break;
case jtokentype.timespan:
curluatable.additem(item.key, (float)item.value);
break;
}
}
return curluatable;
}
static luatable jsonarray2luatable(jarray jsonarray, int layer)
{
var curluatable = new luatable(layer);
//构建有序table(lua表)
foreach (var item in jsonarray)
{
//检查子项类型
switch (item.type)
{
case jtokentype.boolean:
curluatable.additem((bool)item);
break;
case jtokentype.array:
curluatable.additem(jsonarray2luatable(item.toobject<jarray>(), layer + 1));
break;
case jtokentype.string:
curluatable.additem((string)item);
break;
case jtokentype.object:
curluatable.additem(jsonobject2luatable(item.toobject<jobject>(), layer + 1));
break;
case jtokentype.date:
curluatable.additem((string)item);
break;
case jtokentype.float:
curluatable.additem((float)item);
break;
case jtokentype.integer:
curluatable.additem((int)item);
break;
case jtokentype.none:
curluatable.additemnil();
break;
case jtokentype.null:
curluatable.additemnil();
break;
case jtokentype.timespan:
curluatable.additem((float)item);
break;
}
}
return curluatable;
}
}
}using system;
using system.collections.generic;
using system.text;
namespace milk.json
{
public class luaobject
{
private luavaluetype type;
private float valuenumber;
private string valuestring;
private bool valueboolean;
private luatable valuetable;
public luaobject(string value)
{
type = luavaluetype.string;
valuestring = value;
}
public luaobject(float value)
{
type = luavaluetype.num;
valuenumber = value;
}
public luaobject(int value)
{
type = luavaluetype.num;
valuenumber = value;
}
public luaobject(bool value)
{
type = luavaluetype.boolean;
valueboolean = value;
}
public luaobject(luatable value)
{
type = luavaluetype.table;
valuetable = value;
}
public luaobject()
{
type = luavaluetype.nil;
}
public string getstring(bool indented = false)
{
switch (type)
{
case luavaluetype.table:
return valuetable.getstring(indented);
//break;
case luavaluetype.boolean:
if (valueboolean)
{
return "true";
}
else
{
return "false";
}
//break;
case luavaluetype.num:
return valuenumber.tostring();
//break;
case luavaluetype.string:
return "\""+ valuestring+"\"";
case luavaluetype.nil:
return "nil";
default:
return "";
}
}
public luavaluetype getluavaluetype()
{
return type;
}
}
}到此这篇关于基于c#实现json转lua的操作指南的文章就介绍到这了,更多相关c# json转lua内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论