LZ 试试这个吧[Serializable] public class Menu { public BaseButton[] button; } [XmlInclude(typeof(ViewButton))] [XmlInclude(typeof(ClickButton))] [XmlInclude(typeof(SubButton))] [Serializable] public abstract class BaseButton { public string name; } [Serializable] [XmlInclude(typeof(ViewButton))] [XmlInclude(typeof(ClickButton))] public class Button : BaseButton { public string type; } [Serializable] public class ClickButton : Button { public string key; } [Serializable] public class ViewButton : Button { public string url; } [Serializable] public class SubButton : BaseButton { public Button[] sub_button; } public class Class1 { public static Menu GetMenu() { Menu menu = new Menu() { // 一级菜单赋值 button = new BaseButton[] { new ClickButton(){name="1",key="V1001_1"}, new ClickButton(){name="2",key="V1001_02"}, new ClickButton(){name="3",key="V1001_3"}, new SubButton() { name="菜单",sub_button=new Button[] { //左边二级菜单赋值 new ViewButton(){name="1",url="1"}, new ViewButton(){name="2",url="2"}, new ViewButton(){name="3",url="3"} } } } }; return menu; } }
using System; using System.Collections.Generic; using Newtonsoft.Json;namespace TestJsonApi { class Program { static void Main(string[] args) { TestJs(); } public static void TestJs() { var b = new Book(); b.BookId = Guid.NewGuid().ToString(); //b.Name = "Pro C# 2010"; b.SizeLists = new List<string>() { "SmallList", "MiddleList", "LargeList" };
var b2 = new Book(); b2.BookId = Guid.NewGuid().ToString(); b2.Name = "Pro C++ 2010"; //b2.SizeLists = new List<string>() { "SmallList", "MiddleList", "LargeList" };
var Books = new List<Book>(); Books.Add(b); Books.Add(b2); var settings = new JsonSerializerSettings(); settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; settings.NullValueHandling = NullValueHandling.Ignore;//忽略空值。 string output = JsonConvert.SerializeObject(Books, Formatting.Indented, settings); Console.WriteLine(output); Console.Read(); } } [JsonObject(IsReference = true)] public class Book { [JsonProperty] public string BookId { get; set; } public string Name { get; set; } public IList<string> SizeLists { get; set; } } }
button = new BaseButton[] { new ClickButton(){type="click",name=txtMenuLeftOne.Text,key="V1001_1"}, new ClickButton(){type="click",name=txtMenuCentenOne.Text,key="V1001_02"}, new ClickButton(){type="click",name=txtMenuRightOne.Text,key="V1001_3"}, new SubButton() { name="菜单",sub_button=new ButtonBase[] { //左边二级菜单赋值 new ViewButton(){type="view",name=txtMenuLeftTwo.Text,url=txtMenuLeftUrlTwo.Text}, new ViewButton(){type="view",name=txtMenuLeftThree.Text,url=txtMenuLeftUrlThree.Text}, new ViewButton(){type="view",name=txtMenuLeftFour.Text,url=txtMenuLeftUrlFour.Text}, new ViewButton(){type="view",name=txtMenuLeftFive.Text,url=txtMenuLeftUrlFive.Text}, new ViewButton(){type="view",name=txtMenuLeftSix.Text,url=txtMenuLeftUrlSix.Text}, //中间二级菜单赋值 new ViewButton(){type="view",name=txtMenuCentenTwo.Text,url=txtMenuCentenUrlTwo.Text}, new ViewButton(){type="view",name=txtMenuCentenThree.Text,url=txtMenuCentenUrlThree.Text}, new ViewButton(){type="view",name=txtMenuCentenFour.Text,url=txtMenuCentenUrlFour.Text}, new ViewButton(){type="view",name=txtMenuCentenFive.Text,url=txtMenuCentenUrlFive.Text}, new ViewButton(){type="view",name=txtMenuCentenSix.Text,url=txtMenuCentenUrlSix.Text}, //右边二级菜单 new ViewButton(){type="view",name=txtMenuRightTwo.Text,url=txtMenuRightUrlTwo.Text}, new ViewButton(){type="view",name=txtMenuRightThree.Text,url=txtMenuRightUrlThree.Text}, new ViewButton(){type="view",name=txtMenuRightFour.Text,url=txtMenuRightUrlFour.Text}, new ViewButton(){type="view",name=txtMenuRightFive.Text,url=txtMenuRightUrlFive.Text}, new ViewButton(){type="view",name=txtMenuRightSix.Text,url=txtMenuRightUrlSix.Text}, } } } 错误 1无法将类型“wechatForm.ViewButton”隐式转换为“System.Windows.Forms.ButtonBase”new ViewButton() 说不能隐式转换为 System.Windows.Forms.ButtonBase”。可是后面是有继承关系的啊,我搞不明白了 public class ViewButton : Button { public string url; } public class Button : BaseButton { public string type; }
不懂 怎么定义 那JSON 示例的实体类,,
楼主,我那个回复有图有真相啊。 最重要的是这两句: var settings = new JsonSerializerSettings();
试试fastCSharp public class Menu { public Button[] button; } public class Button { public string type = "click"; public string name; [fastCSharp.setup.cSharp.json(IsIgnoreDefaultValue = true)] public string key; [fastCSharp.setup.cSharp.json(IsIgnoreDefaultValue = true)] public Sub_button[] sub_button; } public class Sub_button { public string type = "view"; public string name; [fastCSharp.setup.cSharp.json(IsIgnoreDefaultValue = true)] public string url; } string json = fastCSharp.setup.cSharp.json.toJson.Get(menu);
public class Menu
{
public BaseButton[] button;
} [XmlInclude(typeof(ViewButton))]
[XmlInclude(typeof(ClickButton))]
[XmlInclude(typeof(SubButton))]
[Serializable]
public abstract class BaseButton
{
public string name;
} [Serializable]
[XmlInclude(typeof(ViewButton))]
[XmlInclude(typeof(ClickButton))]
public class Button : BaseButton
{
public string type;
}
[Serializable]
public class ClickButton : Button
{
public string key;
}
[Serializable]
public class ViewButton : Button
{
public string url;
}
[Serializable]
public class SubButton : BaseButton
{
public Button[] sub_button;
} public class Class1
{
public static Menu GetMenu()
{
Menu menu = new Menu()
{
// 一级菜单赋值
button = new BaseButton[]
{
new ClickButton(){name="1",key="V1001_1"},
new ClickButton(){name="2",key="V1001_02"},
new ClickButton(){name="3",key="V1001_3"},
new SubButton()
{
name="菜单",sub_button=new Button[]
{
//左边二级菜单赋值
new ViewButton(){name="1",url="1"},
new ViewButton(){name="2",url="2"},
new ViewButton(){name="3",url="3"}
}
}
}
};
return menu;
}
}
using System.Collections.Generic;
using Newtonsoft.Json;namespace TestJsonApi
{
class Program
{
static void Main(string[] args)
{
TestJs();
} public static void TestJs()
{
var b = new Book();
b.BookId = Guid.NewGuid().ToString();
//b.Name = "Pro C# 2010";
b.SizeLists = new List<string>() { "SmallList", "MiddleList", "LargeList" };
var b2 = new Book();
b2.BookId = Guid.NewGuid().ToString();
b2.Name = "Pro C++ 2010";
//b2.SizeLists = new List<string>() { "SmallList", "MiddleList", "LargeList" };
var Books = new List<Book>();
Books.Add(b);
Books.Add(b2); var settings = new JsonSerializerSettings(); settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
settings.NullValueHandling = NullValueHandling.Ignore;//忽略空值。
string output = JsonConvert.SerializeObject(Books, Formatting.Indented, settings);
Console.WriteLine(output);
Console.Read();
}
} [JsonObject(IsReference = true)]
public class Book
{
[JsonProperty]
public string BookId { get; set; }
public string Name { get; set; }
public IList<string> SizeLists { get; set; }
}
}
{
new ClickButton(){type="click",name=txtMenuLeftOne.Text,key="V1001_1"},
new ClickButton(){type="click",name=txtMenuCentenOne.Text,key="V1001_02"},
new ClickButton(){type="click",name=txtMenuRightOne.Text,key="V1001_3"},
new SubButton()
{
name="菜单",sub_button=new ButtonBase[]
{
//左边二级菜单赋值
new ViewButton(){type="view",name=txtMenuLeftTwo.Text,url=txtMenuLeftUrlTwo.Text},
new ViewButton(){type="view",name=txtMenuLeftThree.Text,url=txtMenuLeftUrlThree.Text},
new ViewButton(){type="view",name=txtMenuLeftFour.Text,url=txtMenuLeftUrlFour.Text},
new ViewButton(){type="view",name=txtMenuLeftFive.Text,url=txtMenuLeftUrlFive.Text},
new ViewButton(){type="view",name=txtMenuLeftSix.Text,url=txtMenuLeftUrlSix.Text},
//中间二级菜单赋值
new ViewButton(){type="view",name=txtMenuCentenTwo.Text,url=txtMenuCentenUrlTwo.Text},
new ViewButton(){type="view",name=txtMenuCentenThree.Text,url=txtMenuCentenUrlThree.Text},
new ViewButton(){type="view",name=txtMenuCentenFour.Text,url=txtMenuCentenUrlFour.Text},
new ViewButton(){type="view",name=txtMenuCentenFive.Text,url=txtMenuCentenUrlFive.Text},
new ViewButton(){type="view",name=txtMenuCentenSix.Text,url=txtMenuCentenUrlSix.Text},
//右边二级菜单
new ViewButton(){type="view",name=txtMenuRightTwo.Text,url=txtMenuRightUrlTwo.Text},
new ViewButton(){type="view",name=txtMenuRightThree.Text,url=txtMenuRightUrlThree.Text},
new ViewButton(){type="view",name=txtMenuRightFour.Text,url=txtMenuRightUrlFour.Text},
new ViewButton(){type="view",name=txtMenuRightFive.Text,url=txtMenuRightUrlFive.Text},
new ViewButton(){type="view",name=txtMenuRightSix.Text,url=txtMenuRightUrlSix.Text},
}
}
}
错误
1无法将类型“wechatForm.ViewButton”隐式转换为“System.Windows.Forms.ButtonBase”new ViewButton() 说不能隐式转换为 System.Windows.Forms.ButtonBase”。可是后面是有继承关系的啊,我搞不明白了
public class ViewButton : Button
{
public string url;
} public class Button : BaseButton
{
public string type;
}
不懂 怎么定义 那JSON 示例的实体类,,
最重要的是这两句:
var settings = new JsonSerializerSettings();
settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
settings.NullValueHandling = NullValueHandling.Ignore;//忽略空值。
string output = JsonConvert.SerializeObject(Books, Formatting.Indented, settings);
Console.WriteLine(output);
{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"type":"click",
"name":"歌手简介",
"key":"V1001_TODAY_SINGER"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"view",
"name":"视频",
"url":"http://v.qq.com/"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}lz你没发现你的json不规则嘛? 你的button是一个数组,button我简称B
B[0] 和B[1]是一个类型,然而B[2]却是另一个类型,这样的jison转换是不会成功的对咯 json序列化不需要加 [Serializable] 标签对于你这样的json只能手动自己解析了,最好弄规则json序列化!!
{
public Button[] button;
}
public class Button
{
public string type = "click";
public string name;
[fastCSharp.setup.cSharp.json(IsIgnoreDefaultValue = true)]
public string key;
[fastCSharp.setup.cSharp.json(IsIgnoreDefaultValue = true)]
public Sub_button[] sub_button;
}
public class Sub_button
{
public string type = "view";
public string name;
[fastCSharp.setup.cSharp.json(IsIgnoreDefaultValue = true)]
public string url;
}
string json = fastCSharp.setup.cSharp.json.toJson.Get(menu);