C# 用于多数据存储和easy.ToJSON()的ASP.NET命名空间控件

C# 用于多数据存储和easy.ToJSON()的ASP.NET命名空间控件,c#,asp.net,json,list,hashtable,C#,Asp.net,Json,List,Hashtable,我需要存储一个列表或哈希表或类似以下内容: Element[542]["Title"] = "Some Title"; Element[542]["Html"] = "some Value"; Element[542]["CSS"] = "some class css"; Element[542]["SomethingElse"] = something else goes in here. Element[621]["Title"] = "Title for 621"; Element[62

我需要存储一个列表或哈希表或类似以下内容:

Element[542]["Title"] = "Some Title";
Element[542]["Html"] = "some Value";
Element[542]["CSS"] = "some class css";
Element[542]["SomethingElse"] = something else goes in here.

Element[621]["Title"] = "Title for 621";
Element[621]["Html"] = "Some Html value for 621";
Element[621]["CSS"] = "Some CSS Class";
Element[621]["SomethingElse"] = "something else goes in here."
AddMoreJsonToThisVariable.621 = {

    Title : "Title for 621",
    Html : "Some Html value for 621",
    CSS :  "Some CSS Class",
    SomethingElse : "something else goes in here."
}
如何创建这样的列表?如果我能根据数字的值将其转换为json,那就太好了,例如,621将是值,因此它将只获取该数字的值,并将其输出到json变量中,如下所示:

Element[542]["Title"] = "Some Title";
Element[542]["Html"] = "some Value";
Element[542]["CSS"] = "some class css";
Element[542]["SomethingElse"] = something else goes in here.

Element[621]["Title"] = "Title for 621";
Element[621]["Html"] = "Some Html value for 621";
Element[621]["CSS"] = "Some CSS Class";
Element[621]["SomethingElse"] = "something else goes in here."
AddMoreJsonToThisVariable.621 = {

    Title : "Title for 621",
    Html : "Some Html value for 621",
    CSS :  "Some CSS Class",
    SomethingElse : "something else goes in here."
}

ASP.NET中是否有任何东西可以用来生成这样的列表?或者是一种做这件事的方法?能否将哈希表插入另一个哈希表的第二个参数中?这行吗?

我建议您创建一个类

public class MyClass
{
    public int Id {get; set;}
    public string Title {get;set;}
    public string Html {get;set;}
    public string CSS {get;set;}
    public string SomethingElse {get;set;}
}

//Then create a dictionary.
Dictionary<int, MyClass> MyDictionary=new Dictionary<int, MyClass)();
// create an some instance of MyClass
MyClass MyClassInstance=new MyClass(){Id=621, Title="A Title", Html="Some Html", CSS= "Some CSS", SomethingElse="Something else goes here"};
//add it to your dictionary
MyDictionary.Add(MyClassInstance.Id, MyClassInstance);
//serialize it to JSON with Json.Net
string somejson= JsonConvert.SerializeObject(MyDictionary);
公共类MyClass
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串Html{get;set;}
公共字符串CSS{get;set;}
公共字符串SomethingElse{get;set;}
}
//然后创建一个字典。
Dictionary MyDictionary=新字典x.Key)
{
WriteLine(“Key为:+kvp.Key+”,Html为:+kvp.Value.Html);
}

我以为你想要的是词典。如果没有,请找出要使用的内容。

通常我们不使用哈希表。我们使用泛型列表或字典类。是一个广泛使用的库,用于将它们转换为JSON。我已经有了一个类。这就是我获取数据的方式,但我需要先将其切换到ID格式的JSon变量。谢谢,这看起来正是我需要的!!只要字典项首先按ID排序(什么将被放入第一个参数中)!另外,好奇的是,我是否必须创建一个类,或者除了类之外还可以创建其他类?在文档中表示值集合,但不确定这实际上意味着什么……您可以在其中放置一个结构或基本类型。这本词典天生就没有分类。但当您访问它时,您可以对其进行排序。我会用一个例子更新我的答案。排序一点都不重要。我只需要访问Javascript中的ID,并让它以JSON输出该ID的所有属性。但是ID并不总是相同的,因此它需要加载所有可能的ID,并将相同的属性附加到这些ID上。