繁体   English   中英

从QueryString c#获取Json对象

[英]Get Json Object From QueryString c#

我发现最好的是,堆栈上的可用解决方案没有嵌套json对象的答案 ,它们仅解决线性json值。 但是我有要发送的数据就像

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

如果我想用php做,我会做

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

但是在C#中我无法做到这一点 因此,如果有任何内置方法,我将不胜感激,并且希望得到帮助,以修复我的以下代码

我想制作一个嵌套字典(不过我更喜欢JOBject)。 为了便于显示输出,我将结果序列化了,

我从以下代码获得的结果是{"a":"78","ob":{},"ob.b":{"a":"gffg"},"ob.bh":{"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;
    }

以下方法为您提供了所有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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM