获取Json数据某节点的值
2021-01-18 18:16
标签:static ons turn sel dex col XML public null 获取Json数据某节点的值 标签:static ons turn sel dex col XML public null 原文地址:https://www.cnblogs.com/HuiShouGuoQu/p/13343110.htmlpublic 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 GetXMLstrByKey(string Key, XmlDocument xml)
{
object strValue = xml.SelectSingleNode("xml").SelectSingleNode(Key).InnerText;
if (strValue != null)
{
return strValue.ToString();
}
else
{
return "";
}
}