19,502
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 把返回字符串按照Json解析为一个HashTable
/// </summary>
/// <param name="pString">Json字符串</param>
/// <returns>Key->Value的HashTable</returns>
public static Hashtable SimpleJson(string pString)
{
Hashtable ht = new Hashtable();
//Step1 去除前后的"{"和"}";
if (pString.Length < 2)
{
return ht;
}
else if (pString.Substring(0, 1) != "{" && pString.Substring(pString.Length - 1, 1) != "}")
{
return ht;
}
else
{
//content为去掉{ }的内容
string content = pString.Substring(1, pString.Length - 2);
//对conent以","分割
string[] items = content.Split(',');
//对items以":"分割
for (int i = 0; i < items.Length; i++)
{
string[] keyandvalue = items[i].Split(':');
if (keyandvalue.Length== 2)
{
string key = keyandvalue[0].Replace("'","");
string value = keyandvalue[1];
if (!ht.ContainsKey(key))
ht.Add(key, value);
else
ht[key] = value;
}
}
}
return ht;
}