json转换为键值对辅助类

 /// <summary>
/// json转换为键值对辅助类
/// </summary>
public class JsonParser
{ private static Dictionary<string, string> lst_KeyValueData = null; public static Dictionary<string, string> SplitJsonStringToKeyValuePairs(string jsonStr)
{
char jsonBeginToken = '{';
char jsonEndToken = '}'; if (string.IsNullOrEmpty(jsonStr))
{
return null;
}
//验证json字符串格式
if (jsonStr[] != jsonBeginToken || jsonStr[jsonStr.Length - ] != jsonEndToken)
{
throw new ArgumentException("非法的Json字符串!");
} lst_KeyValueData = new Dictionary<string, string>(); JObject jobj = new JObject(); // 转换为json对象
jobj = JObject.Parse(jsonStr);
ParseJsonProperties(jobj); return lst_KeyValueData; } private static void ParseJsonProperties(JObject jObject)
{
IEnumerable<JProperty> jObject_Properties = jObject.Properties(); JTokenType[] validPropertyValueTypes = { JTokenType.String, JTokenType.Integer, JTokenType.Float, JTokenType.Boolean, JTokenType.Null, JTokenType.Date, JTokenType.Bytes, JTokenType.Guid, JTokenType.Uri, JTokenType.TimeSpan };
List<JTokenType> propertyTypes = new List<JTokenType>(validPropertyValueTypes); JTokenType[] validObjectTypes = { JTokenType.String, JTokenType.Array, JTokenType.Object };
List<JTokenType> objectTypes = new List<JTokenType>(validObjectTypes); foreach (JProperty property in jObject_Properties)
{ try
{
if (propertyTypes.Contains(property.Value.Type))
{
ParseJsonKeyValue(property, property.Name.ToString());
}
else if (objectTypes.Contains(property.Value.Type))
{
//Arrays ex. { names: ["first": "John", "last" : "doe"]}
if (property.Value.Type == JTokenType.Array && property.Value.HasValues)
{
ParseJsonArray(property);
} //Objects ex. { name: "john"}
if (property.Value.Type == JTokenType.Object)
{
JObject jo = new JObject();
jo = JObject.Parse(property.Value.ToString());
string paramName = property.Name.ToString(); lst_KeyValueData.Add(paramName, property.Value.ToString()); if (jo.HasValues)
{
ParseJsonProperties(jo);
} }
}
}
catch (Exception ex)
{
throw;
}
} // End of ForEach } private static void ParseJsonKeyValue(JProperty item, string paramName)
{
lst_KeyValueData.Add(paramName, item.Value.ToString());
} private static void ParseJsonArray(JProperty item)
{
JArray jArray = (JArray)item.Value; string paramName = item.Name.ToString();
lst_KeyValueData.Add(paramName, item.Value.ToString()); try
{
for (int i = ; i < jArray.Count; i++)
{ paramName = i.ToString();
lst_KeyValueData.Add(paramName, jArray.Values().ElementAt(i).ToString()); JObject jo = new JObject();
jo = JObject.Parse(jArray[i].ToString());
IEnumerable<JProperty> jArrayEnum = jo.Properties(); foreach (JProperty jaItem in jArrayEnum)
{
var paramNameWithJaItem = jaItem.Name.ToString(); var itemValue = jaItem.Value.ToString(Formatting.None);
if (itemValue.Length > )
{
switch (itemValue.Substring(, ))
{
case "[":
//Recusion call to itself
ParseJsonArray(jaItem);
break;
case "{":
//Create a new JObject and parse
JObject joObject = new JObject();
joObject = JObject.Parse(itemValue); //For this value, reparse from the top
ParseJsonProperties(joObject);
break;
default:
ParseJsonKeyValue(jaItem, paramNameWithJaItem);
break;
}
}
}
} //end for loop }
catch (Exception ex)
{
throw;
}
}
}
上一篇:Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp


下一篇:Java抽象类和内部类