获取Json数据某节点的值


public static string GetJsonValue(string jsonStr, string key) { string result = string.Empty; if (!string.IsNullOrEmpty(jsonStr)) { key = "\"" + key.Trim('"') + "\""; int index = jsonStr.IndexOf(key) + key.Length + 1; if (index > key.Length + 1) { //先截逗号,若是最后一个,截“}”号,取最小值 int end = jsonStr.IndexOf(',', index); if (end == -1) { end = jsonStr.IndexOf('}', index); } result = jsonStr.Substring(index, end - index); result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格  } } return result; }

--上面的不规范,参考下面写法
  public static String GetJsonValue(String jsonStr, String key){
        String result ="";
        if (!jsonStr.equals("")){
            key = "\"" + key.trim() + "\"";
            int index = jsonStr.indexOf(key) + key.length() + 1;
            if (index > key.length() + 1){
                //先截逗号,若是最后一个,截“}”号,取最小值
                int end = jsonStr.indexOf(',', index);
                if (end == -1){
                    end = jsonStr.indexOf('}', index);
                }
 
                result = jsonStr.substring(index, end - index);
                result = result.trim(); //过滤引号或空格
            }
        }
        return result;
    }


public static string GetXMLstrByKey(string Key, XmlDocument xml) { object strValue = xml.SelectSingleNode("xml").SelectSingleNode(Key).InnerText; if (strValue != null) { return strValue.ToString(); } else { return ""; } }
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM