JObject遍历Json格式属性和值

àì夳堔傛蜴生んèń 2023-10-07 11:38 63阅读 0赞

经常在Unity里要写Json格式解析,固定的类直接写类解析就好。

  1. CData getParm = (CData )JsonConvert.DeserializeObject(result, typeof(CData));

动态的,不知道属性名的,总是忘了代码怎么写,放博客里记录下。

把字符串转化成JObject对象

  1. JObject obj = (JObject)JsonConvert.DeserializeObject(jsonStr);

JProperty可以获取到JToken或者JObject的属性名。

  1. foreach (JProperty item in obj.Properties())
  2. //或者
  3. foreach (JProperty item in token)

下面来写一个测试的例子。

Json数据如下:

  1. {"skeleton":{"images":""},
  2. "bones":[{"name":"root"}],
  3. "skins":{
  4. "default":{
  5. },
  6. "base":{
  7. "hair":{"hair":{"name":"base/hair", "x":1424,"y":2564.5,"width":1132,"height":1177}},
  8. "h_r":{"h_r":{"name":"base/h_r", "x":1166.5,"y":1955,"width":313,"height":280}},
  9. "0":{"0":{"name":"base/0", "x":1455.5,"y":1468.5,"width":2197,"height":2813}},
  10. "00":{"00":{"name":"base/00", "x":1421.5,"y":2065.5,"width":703,"height":117}}
  11. },
  12. "mouth":{
  13. "m_light":{"m_light":{"name":"mouth/m_light", "x":1415,"y":1616.5,"width":178,"height":115}},
  14. "m_2":{"m_2":{"name":"mouth/m_2", "x":1417.5,"y":1574,"width":331,"height":102}},
  15. "m_1":{"m_1":{"name":"mouth/m_1", "x":1420,"y":1639,"width":324,"height":68}}
  16. },
  17. "eye_ball":{
  18. "e_l":{"e_l":{"name":"eye_ball/e_l", "x":1673,"y":2056.5,"width":112,"height":103}},
  19. "e_r":{"e_r":{"name":"eye_ball/e_r", "x":1404.5,"y":2055,"width":571,"height":100}}
  20. },
  21. "hair":{
  22. "h_1":{"h_1":{"name":"hair/h_1", "x":1463.5,"y":1511.5,"width":1897,"height":2901}}
  23. }
  24. },
  25. "animations":{"animation":{}}
  26. }

目标要读取skins里的数据

  1. static string SpritePath(string url)
  2. {
  3. return Application.dataPath +"/"+ url;
  4. }
  5. //1536 * 2268
  6. //(768 , 1134)
  7. static Vector2 SpriteSize = new Vector2(1536f, 2268f);
  8. static Vector2 SpriteCenter = SpriteSize * 0.5f;
  9. static int SpriteScale = 100;
  10. static string spath = "";
  11. [MenuItem("工具/生成模板")]
  12. static void ImportDresserMask()
  13. {
  14. //首先读取json
  15. string dataPath;//= System.IO.Path.GetFullPath(".");
  16. dataPath = SpritePath("AssetAll/ps/1") ;
  17. string fullPath = dataPath+ "/1.json";
  18. GameObject objRoot = new GameObject("root");
  19. objRoot.transform.localPosition = Vector3.zero;
  20. if (File.Exists(fullPath))
  21. {
  22. StreamReader sr = new StreamReader(fullPath, System.Text.Encoding.UTF8);
  23. string jsonStr = sr.ReadToEnd();
  24. sr.Close();
  25. JObject obj = (JObject)JsonConvert.DeserializeObject(jsonStr);
  26. JObject state = (JObject)obj["skins"]; //获取skins里的数据
  27. //foreach (JToken item in state.Values())
  28. int order = 0;
  29. foreach (JProperty item in state.Properties())
  30. {
  31. string pkey = item.Name;
  32. Debug.Log("分类:" + pkey);
  33. JToken tok = state[pkey];
  34. if (tok.HasValues)
  35. {
  36. GameObject objclass = new GameObject(pkey);
  37. objclass.transform.SetParent(objRoot.transform);
  38. objclass.transform.localPosition = Vector3.zero;
  39. //循环所有属性
  40. foreach (JProperty jp in tok)
  41. {
  42. order++;
  43. string k = jp.Name;
  44. JToken v = jp.Value;
  45. JObject o = (JObject)v[k];
  46. GameObject ob = new GameObject(k);
  47. ob.transform.SetParent(objclass.transform);
  48. Vector2 at = (new Vector2((float)o["x"], (float)o["y"]) - SpriteCenter)/ SpriteScale;
  49. ob.transform.localPosition = at;
  50. SpriteRenderer spriteRenderer = ob.AddComponent<SpriteRenderer>();
  51. spriteRenderer.sortingOrder = order;
  52. string imgPath = dataPath + "/" + o["name"] + ".png";
  53. //Texture2D myTexture = new Texture2D(1, 1);
  54. //myTexture.LoadImage(System.IO.File.ReadAllBytes(imgPath));
  55. //myTexture.Apply();
  56. //Sprite sp = Sprite.Create(myTexture, new Rect(0f, 0f, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f));
  57. Sprite sp = AssetDatabase.LoadAssetAtPath<Sprite>(imgPath);
  58. spriteRenderer.sprite = sp;
  59. Debug.Log(k+" - "+o["name"]+","+o["width"] );
  60. }
  61. }
  62. }
  63. }
  64. }

输出结果

  1. hair - base/hair,1132
  2. h_r - base/h_r,313
  3. 0 - base/0,2197
  4. 00 - base/00,703
  5. m_light - mouth/m_light,178
  6. ..........
  7. ..........

发表评论

表情:
评论列表 (有 0 条评论,63人围观)

还没有评论,来说两句吧...

相关阅读