c#-Json-Json字符串字段递归排序

0
9
 private static dynamic GetSortObject(Object obj)
{
    if (obj is JValue)
    {
        return (obj as JValue)?.Value;
    }
    else if (obj is JArray)
    {
        var list = new List<dynamic>();
        foreach (var item in (obj as JArray))
        {
            list.Add(GetSortObject(item));
        }
        return list;
    }
    else if (obj is JObject)
    {
        var sourceJson = JsonConvert.SerializeObject(obj);
        var paramDic = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(sourceJson);
        var newDic = new SortedDictionary<string, dynamic>();
        for (int i = 0; i < paramDic.Count; i++)
        {
            newDic.Add(paramDic.ElementAt(i).Key, GetSortObject(paramDic.ElementAt(i).Value));
        }
        return newDic;
    }
    else
    {
        return obj;
    }
}

<

发布回复

请输入评论!
请输入你的名字