当前位置:   article > 正文

JObject遍历Json格式属性和值

jobject

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

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

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

把字符串转化成JObject对象

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

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

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

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

Json数据如下:

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

目标要读取skins里的数据

static string SpritePath(string url)
    {
        return Application.dataPath +"/"+ url;
    }

    //1536 * 2268
    //(768 , 1134)
    static Vector2 SpriteSize = new Vector2(1536f, 2268f);
    static Vector2 SpriteCenter = SpriteSize * 0.5f;
    static int SpriteScale = 100;
    static string spath = "";
    [MenuItem("工具/生成模板")]
    static void ImportDresserMask()
    {
        //首先读取json 

        string dataPath;//= System.IO.Path.GetFullPath(".");
        dataPath = SpritePath("AssetAll/ps/1") ;
        string fullPath = dataPath+ "/1.json";

        GameObject objRoot = new GameObject("root");
        objRoot.transform.localPosition = Vector3.zero;


        if (File.Exists(fullPath))
        {
            StreamReader sr = new StreamReader(fullPath, System.Text.Encoding.UTF8);
            string jsonStr = sr.ReadToEnd();
            sr.Close();

            JObject obj = (JObject)JsonConvert.DeserializeObject(jsonStr);
            JObject state = (JObject)obj["skins"];  //获取skins里的数据
            //foreach (JToken item in state.Values())
            int order = 0;
            foreach (JProperty item in state.Properties())
            {
                string pkey = item.Name;
                
                Debug.Log("分类:" + pkey);
                JToken tok = state[pkey];
                if (tok.HasValues)
                {
                    GameObject objclass = new GameObject(pkey);
                    objclass.transform.SetParent(objRoot.transform);
                    objclass.transform.localPosition = Vector3.zero;

                    //循环所有属性
                    foreach (JProperty jp in tok)
                    {
                        order++;
                        string k = jp.Name;
                        JToken v = jp.Value;
                        JObject o = (JObject)v[k];

                        GameObject ob = new GameObject(k);
                        ob.transform.SetParent(objclass.transform);
                        Vector2 at = (new Vector2((float)o["x"], (float)o["y"]) - SpriteCenter)/ SpriteScale;
                        ob.transform.localPosition = at;
                        SpriteRenderer spriteRenderer = ob.AddComponent<SpriteRenderer>();
                        spriteRenderer.sortingOrder = order;

                        string imgPath = dataPath + "/" + o["name"] + ".png";
                        //Texture2D myTexture = new Texture2D(1, 1);
                        //myTexture.LoadImage(System.IO.File.ReadAllBytes(imgPath));
                        //myTexture.Apply();
                        //Sprite sp = Sprite.Create(myTexture, new Rect(0f, 0f, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f));
                        Sprite sp = AssetDatabase.LoadAssetAtPath<Sprite>(imgPath);
                        spriteRenderer.sprite = sp;

                        Debug.Log(k+" - "+o["name"]+","+o["width"] );
                    }
                }
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

输出结果

hair - base/hair,1132
h_r - base/h_r,313
0 - base/0,2197
00 - base/00,703
m_light - mouth/m_light,178
..........
..........
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/123450
推荐阅读
相关标签
  

闽ICP备14008679号