using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace Game.JSON
{
public static class AnalyzeJSON
{
///
/// 把json数据解析到一个字典里面,必须保证key不会重复
///
///
public static void AnalyzeToDic(string data)
{
// Dictionary dictionary = new Dictionary();
//
// var deserializeObject = JsonConvert.DeserializeAnonymousType(data, new Dictionary());
//
// Deep(deserializeObject, ref dictionary);
// 解析 JSON 到动态类型
var result = JsonConvert.DeserializeObject>(data);
// 创建最终存储用的字典
var finalDictionary = new Dictionary();
// foreach (var item in result)
// {
// if (item.Value is JArray)
// {
// // 序列化嵌套数组或对象为 JSON 字符串
// finalDictionary[item.Key] = JsonConvert.SerializeObject(item.Value, Formatting.None);
// }
// else
// {
// // 直接将值转换为字符串
// finalDictionary[item.Key] = item.Value.ToString();
// }
// }
Deep(result, ref finalDictionary);
// 打印最终字典中的内容
foreach (var pair in finalDictionary)
{
Debug.Log($"{pair.Key}: {pair.Value}");
}
}
static void Deep(Dictionary dic1, ref Dictionary dic2)
{
foreach (var item in dic1)
{
if (item.Value is JArray)
{
// 序列化嵌套数组或对象为 JSON 字符串
var str = JsonConvert.SerializeObject(item.Value, Formatting.None);
var deserializeObject = JsonConvert.DeserializeObject>(str);
Deep(deserializeObject, ref dic2);
}
else
{
// 直接将值转换为字符串
dic2[item.Key] = item.Value.ToString();
}
}
}
#region 解析到字典里面
///
/// 解析到一个字典的结构类型的里面去
///
///
public static Dictionary AnalyzeToDicDeep(string json)
{
// 假设JSON字符串是这样的一个结构
string jsonString = json;
JArray jsonArray = JArray.Parse(jsonString);
var resultDictionary = new Dictionary();
ProcessJsonElement(jsonArray, resultDictionary, "Items");
// 输出字典以检查结果
PrintDictionary(resultDictionary, "");
return resultDictionary;
}
private static void ProcessJsonElement(JToken token, Dictionary dict, string key)
{
if (token.Type == JTokenType.Object)
{
var subDict = new Dictionary();
foreach (var prop in ((JObject)token).Properties())
{
ProcessJsonElement(prop.Value, subDict, prop.Name);
}
dict[key] = subDict;
}
else if (token.Type == JTokenType.Array)
{
var list = new List>();
foreach (var item in token.Children())
{
var subDict = new Dictionary();
ProcessJsonElement(item, subDict, "Item");
list.Add(subDict);
}
dict[key] = list;
}
else
{
dict[key] = token.ToString();
}
}
private static void PrintDictionary(Dictionary dict, string indent)
{
foreach (var pair in dict)
{
Debug.Log($"{indent}{pair.Key}:");
if (pair.Value is Dictionary subDict)
{
PrintDictionary(subDict, indent + " ");
}
else if (pair.Value is List> list)
{
foreach (var item in list)
{
PrintDictionary(item, indent + " ");
}
}
else
{
Debug.Log($"{indent} {pair.Value}");
}
}
}
#endregion
// static void Deep(Dictionary dic1, ref Dictionary dic2)
// {
// foreach (var valuePair in dic1)
// {
// var deserializeObject = JsonConvert.DeserializeAnonymousType((string)valuePair.Value, new Dictionary());
// if (deserializeObject != null)
// {
// Deep(deserializeObject, ref dic2);
// }
// else
// {
// dic2.TryAdd(valuePair.Key, valuePair.Value.ToString());
// Debug.Log($"Key: {valuePair.Key}, Value: {valuePair.Value}");
// }
// }
// }
public static void Main(string data)
{
var detailsDictionary = new Dictionary();
JObject jsonObject = JObject.Parse(data);
foreach (var property in jsonObject.Properties())
{
if (property.Value is JArray)
{
detailsDictionary[property.Name] = ProcessArray((JArray)property.Value);
}
else
{
detailsDictionary[property.Name] = property.Value.ToString();
}
}
// Use the detailsDictionary as needed
foreach (var keyValuePair in detailsDictionary)
{
Debug.Log(keyValuePair.Key + " + " + keyValuePair.Value);
}
}
static object ProcessArray(JArray array)
{
var list = new List