如何在ASP.Net中将json转换为可读数组

时间:2014-05-12 05:14:26

标签: asp.net json

我有来自WebService的JSON字符串。 我已经使用此代码将其转换为对象。

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
object cw_values = serializer.DeserializeObject(cw_stringData);

cw_stringData是我的JSON。

它会向我返回object{object[69]}。例如,它意味着它包含69个项目作为其第一级。

我已经通过此代码再次转换它。

object[] final_cw_values = ((object[])(cw_values));

所以现在final_cw_values的类型为object[]

final_cw_values中的每69项都有此类型。

object {System.Collections.Generic.Dictionary<string,object>}

其中有15个项目。

每个内部项目都有类型:

System.Collections.Generic.KeyValuePair<string,object>

包含一个Key和一个Value

我需要在数组中包含这些Value

如果我点击Value并点击Add to Watch,我会有一些像这样的代码,但它在我的代码中不起作用。

(new System.Collections.Generic.Mscorlib_DictionaryDebugView<string,object>(((System.Collections.Generic.Dictionary<string,object>)(final_cw_values[0])))).Items[0].Value

这是我的JSON的一部分:

[
  {
    "FirstWeight": -1,
    "SecondWeight": 0,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:rowspan,3;SecondWeight:rowspan,3;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.2 اندازه در بورس",
    "Hidden2": "202       ",
    "ThirdLevel": "2.2.1 ارزش بازار (م ر)",
    "Hidden3": "10031     ",
    "ProfileCode": "1",
    "ThirdWeight": 22,
    "FirstEffectiveWeight": 50,
    "SecondEffectiveWeight": 25,
    "ThirdEffectiveWeight": 5.5,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.2 اندازه در بورس",
    "Hidden2": "202       ",
    "ThirdLevel": "2.2.2 درصد سهام شناور آزاد (%)",
    "Hidden3": "10036     ",
    "ProfileCode": "1",
    "ThirdWeight": 52,
    "FirstEffectiveWeight": 50,
    "SecondEffectiveWeight": 25,
    "ThirdEffectiveWeight": 13,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.2 اندازه در بورس",
    "Hidden2": "202       ",
    "ThirdLevel": "2.2.3 ارزش بازار شناور آزاد (م ر)",
    "Hidden3": "10053     ",
    "ProfileCode": "1",
    "ThirdWeight": 12,
    "FirstEffectiveWeight": 50,
    "SecondEffectiveWeight": 25,
    "ThirdEffectiveWeight": 3,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": 0,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:rowspan,5;SecondWeight:rowspan,5;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.3 نفدشوندگی سهام در بورس",
    "Hidden2": "203       ",
    "ThirdLevel": "2.3.1 تعداد روزهای بدون معامله اخیر",
    "Hidden3": "10066     ",
    "ProfileCode": "1",
    "ThirdWeight": 2,
    "FirstEffectiveWeight": 50,
    "SecondEffectiveWeight": 25,
    "ThirdEffectiveWeight": 0.5,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.3 نفدشوندگی سهام در بورس",
    "Hidden2": "203       ",
    "ThirdLevel": "2.3.2 تعداد روزهای معامله شده در ​​سال جاری",
    "Hidden3": "10095     ",
    "ProfileCode": "1",
    "ThirdWeight": 0,
    "FirstEffectiveWeight": 0,
    "SecondEffectiveWeight": 0,
    "ThirdEffectiveWeight": 0,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.3 نفدشوندگی سهام در بورس",
    "Hidden2": "203       ",
    "ThirdLevel": "2.3.3 متوسط تعداد روزهای معامله شده در سال",
    "Hidden3": "10131     ",
    "ProfileCode": "1",
    "ThirdWeight": 15,
    "FirstEffectiveWeight": 50,
    "SecondEffectiveWeight": 25,
    "ThirdEffectiveWeight": 3.75,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.3 نفدشوندگی سهام در بورس",
    "Hidden2": "203       ",
    "ThirdLevel": "2.3.4 تعداد معاملات در سال جاری",
    "Hidden3": "10135     ",
    "ProfileCode": "1",
    "ThirdWeight": 0,
    "FirstEffectiveWeight": 0,
    "SecondEffectiveWeight": 0,
    "ThirdEffectiveWeight": 0,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": 0,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:rowspan,4;SecondWeight:rowspan,4;",
    "class": "not-editable-row",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.4 قیمت سهام در بورس",
    "Hidden2": "204       ",
    "ThirdLevel": "2.4.1 قیمت پابانی",
    "Hidden3": "10185     ",
    "ProfileCode": "1",
    "ThirdWeight": 0,
    "FirstEffectiveWeight": 0,
    "SecondEffectiveWeight": 0,
    "ThirdEffectiveWeight": 0,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "class": "not-editable-row",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.4 قیمت سهام در بورس",
    "Hidden2": "204       ",
    "ThirdLevel": "2.4.3 پایین ترین قیمت در 52 هفته گذشته",
    "Hidden3": "10304     ",
    "ProfileCode": "1",
    "ThirdWeight": 0,
    "FirstEffectiveWeight": 0,
    "SecondEffectiveWeight": 0,
    "ThirdEffectiveWeight": 0,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": -1,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:display,none;SecondWeight:display,none;",
    "class": "not-editable-row",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.4 قیمت سهام در بورس",
    "Hidden2": "204       ",
    "ThirdLevel": "2.4.4 متوسط قیمت در 12 هفته گذشته",
    "Hidden3": "10312     ",
    "ProfileCode": "1",
    "ThirdWeight": 0,
    "FirstEffectiveWeight": 0,
    "SecondEffectiveWeight": 0,
    "ThirdEffectiveWeight": 0,
    "Clustering": 1
  },
  {
    "FirstWeight": -1,
    "SecondWeight": 0,
    "attr": "FirstLevel:display,none;FirstWeight:display,none;SecondLevel:rowspan,5;SecondWeight:rowspan,5;",
    "FirstLevel": "2. معیارهای معاملاتی",
    "Hidden1": "2         ",
    "SecondLevel": "2.5 بازدهی سهام در بورس",
    "Hidden2": "205       ",
    "ThirdLevel": "2.5.1 آخرین بازده روزانه (%)",
    "Hidden3": "10186     ",
    "ProfileCode": "1",
    "ThirdWeight": 0,
    "FirstEffectiveWeight": 0,
    "SecondEffectiveWeight": 0,
    "ThirdEffectiveWeight": 0,
    "Clustering": 1
  }
]

穆罕默德

1 个答案:

答案 0 :(得分:3)

如果您创建强类型类,则更好。

public class Data
{
    public int FirstWeight { get; set; }
    public int SecondWeight { get; set; }
    public string attr { get; set; }
    public string FirstLevel { get; set; }
    public string Hidden1 { get; set; }
    public string SecondLevel { get; set; }
    public string Hidden2 { get; set; }
    public string ThirdLevel { get; set; }
    public string Hidden3 { get; set; }
    public string ProfileCode { get; set; }
    public int ThirdWeight { get; set; }
    public int FirstEffectiveWeight { get; set; }
    public int SecondEffectiveWeight { get; set; }
    public double ThirdEffectiveWeight { get; set; }
    public int Clustering { get; set; }
}

转换为

JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Data> lstValues = serializer.Deserialize<List<Data>>(cw_stringData);