獲取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