zoukankan      html  css  js  c++  java
  • Json也可以这么使

    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);
    }
    }

  • 相关阅读:
    JavaWeb开发小结
    第一个Maven案例Hello Maven
    数据字典 dba_free_space及相对文件号RELATIVE_FNO 小结
    执行计划
    五分钟搞死一台服务器
    RAC 移动 OCR
    使用DBMS_STATS来收集统计信息【转】
    关于Freelists和Freelist Groups的研究【转】
    [转]Oracle中INITRANS和MAXTRANS参数
    linux套件安装过程中configure,make,make install的作用
  • 原文地址:https://www.cnblogs.com/hjhd/p/3246147.html
Copyright © 2011-2022 走看看