我发现最好的是堆栈中的可用解决方案没有嵌套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.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 :(得分:2)
以下方法为您提供任何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;
}