从查询字符串获取Json对象c#

问题描述:

我发现的最好结果是堆栈中的可用解决方案没有嵌套json对象的答案,它们只处理班轮json值。但是,我有数据要发送就像从查询字符串获取Json对象c#

{ ob: { a: "78", b: { a: "gffg", h: {m:67, j:"fff"} } } } 

如果我想这样做在PHP我只想做

$json = $_POST['ob']; 
$obj = json_decode($json); 

但在C#我can'not做。所以我会很感激任何内置的方法,如果有的话,我很乐意帮助修复我的下面的代码

我想做一个嵌套字典(我宁愿JOBject虽然)。为了便于显示输出的我已经序列化的结果,

什么结果,我已经从下面的代码实现又是 {"a":"78","ob":{},"ob.b":{"a":"gffg"},"ob.b.h":{"m":"67","j":"fff"}}但期望的结果等发送数据{ "ob": { "a": "78", "b": { "a": "gffg", "h": {m:67, "j":"fff"} } } }代码是

string getJsonStringFromQueryString() 
    { 
     Dictionary<string, object> dic = new Dictionary<string, object>(); 
     var nvc = Request.QueryString; 
     foreach (string key in nvc.Keys) 
     { 
      string[] values = nvc.GetValues(key); 
      string tempKey = key; 
      tempKey = tempKey.Replace("[", ".").Replace("]", ""); 
      if (values.Length == 1) 
       dic.Add(tempKey, values[0]); 
      else 
       dic.Add(tempKey, values); 
     } 

     //It is giving me 
     {[ob.a, 78]} 
     {[ob.b.a, gffg]} 
     {[ob.b.h.m, 67]} 
     {[ob.b.h.j, fff]} 

     var result = makeNestedObject(dic); 
     var json = new JavaScriptSerializer().Serialize(result);  

     return json; 
    } 

我想添加叶键和它们的值,因为它是和所有其他键作为字典

Dictionary<string, object> makeNestedObject(Dictionary<string, object> qsDictionar) 
    { 
     Dictionary<string, object> result = new Dictionary<string, object>(); 
     foreach (string key in qsDictionar.Keys) 
     { 
      string temp = ""; 
      if (key.Contains(".")) 
      { 
       string[] ar = key.Split('.'); 
       if (ar.Length > 2) 
       { 
        for (int i = 0; i < ar.Length - 1; i++) 
        { 
         temp = ar[0]; 
         for (int j = 1; j <= i; j++) 
         { 
          temp += "." + ar[j]; 
         } 
      //above is getting the previous key i want to use as dictionary, leaving the leaf key. 
         try 
         { 
          Dictionary<string, object> forTry = (Dictionary<string, object>)result[temp]; 
         } 
         catch 
         { 
          result.Add(temp, new Dictionary<string, object>()); 
         } 
        } 
        ((Dictionary<string, object>)result[temp]).Add(ar[ar.Length - 1], qsDictionar[key]); 
       } 
       else 
        result.Add(ar[1], qsDictionar[key]); 
      } 
     } 
     return result; 
    } 
+0

我自己使用'使用Newtonsoft.Json.Linq'中的'JObject'来将字符串转换为JSON对象。通过JObject.Parse(json)',您可以通过使用JObject上的['key_name']来访问密钥 –

+0

是的,我们可以。但问题是来自querystring的makinh JObject – Sami

以下方法为您提供任何json对象的完整解决方案。

string getJsonStringFromQueryString() 
    { 
     Dictionary<string, object> dic = new Dictionary<string, object>(); 
     var nvc = Request.QueryString; 
     foreach (string key in nvc.Keys) 
     { 
      string[] values = nvc.GetValues(key); 
      string tempKey = key; 
      tempKey = tempKey.Replace("[", ".").Replace("]", ""); 
      if (values.Length == 1) 
       dic.Add(tempKey, values[0]); 
      else 
       dic.Add(tempKey, values); 
     } 

     string vald = Request.QueryString["ob"]; 

     var result = makeNestedObject(dic); 
     var json = new JavaScriptSerializer().Serialize(result); 
     return json; 
    } 

    Dictionary<string, object> makeNestedObject(Dictionary<string, object> qsDictionar) 
    { 
     Dictionary<string, object> result = new Dictionary<string, object>(); 
     foreach (string key in qsDictionar.Keys) 
     { 
      if (key.Contains(".")) 
      { 
       List<string> keysList = key.Split('.').ToList(); 
       Dictionary<string, object> lastAddedDictionary = result; 
       while (keysList.Count > 1) 
       { 
        if (!lastAddedDictionary.ContainsKey(keysList[0])) 
        { 
         Dictionary<string, object> temp = new Dictionary<string, object>(); 
         lastAddedDictionary[keysList[0]] = temp; 
         lastAddedDictionary = temp; 
        } 
        else       
         lastAddedDictionary = (Dictionary<string, object>)lastAddedDictionary[keysList[0]];       
        keysList.RemoveAt(0); 
       } 
       lastAddedDictionary[keysList[0]] = qsDictionar[key]; 
      } 
      else 
      { 
       result.Add(key, qsDictionar[key]); 
      } 
     } 
     return result; 
    }