110,537
社区成员
发帖
与我相关
我的任务
分享
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);
var settings = new JsonSerializerSettings();
settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
settings.NullValueHandling = NullValueHandling.Ignore;//忽略空值。
string output = JsonConvert.SerializeObject(Books, Formatting.Indented, settings);
Console.WriteLine(output);
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; }
}
}
[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;
}
}