public string GetJSON()
{
//Json数据
string json112 = "{"control_info":{"imei":"854698532597005", "os":"android","os_ver":"4.0","app_ver":"1.0", "cp_ver":"1.0","timestamp":"20130708161023", "user_id":"","operation":"login"},"params":{ "username":"ZhangSan", "password":"123456"}}";
Dictionary<string, object> dic = JsonToDictionary(json112);//将Json数据转成dictionary格式
Dictionary<string, object> dataSet = JsonToDictionary(json112);
//使用KeyValuePair遍历数据
StringBuilder strs = new StringBuilder();
foreach (KeyValuePair<string, object> item in dic)
{
if (item.Key.ToString() == "control_info")//获取header数据
{
var subItem = (Dictionary<string, object>)item.Value;
foreach (var str in subItem)
{
strs.Append(str.Key + ":" + str.Value + "
");
key1 = str.Key;
value1 = str.Value.ToString();
if (key1 == "operation")
{
#region 登录login
if (value1 == "login")
{
foreach (KeyValuePair<string, object> item1 in dataSet)
{
if (item1.Key.ToString() == "params")//获取header数据
{
var subItem1 = (Dictionary<string, object>)item1.Value;
foreach (var str1 in subItem1)
{
strs.Append(str1.Key + ":" + str1.Value + "
");
if (str1.Key == "username")
{
username = str1.Value.ToString();
}
if (str1.Key == "password")
{
password = str1.Value.ToString();
}
}
}
}
Login(username, password);
}
break;
#endregion
}
}
}
}
return strs.ToString();
}
/// <summary>
/// 将json数据反序列化为Dictionary
/// </summary>
/// <param name="jsonData">json数据</param>
/// <returns></returns>
private Dictionary<string, object> JsonToDictionary(string jsonData)
{
//实例化JavaScriptSerializer类的新实例
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
//将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
return jss.Deserialize<Dictionary<string, object>>(jsonData);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}