JObject遍历Json格式属性和值

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

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

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

把字符串转化成JObject对象

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

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

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

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

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":{}}
}

目标要读取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.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(imgPath);
                        spriteRenderer.sprite = sp;

                        Debug.Log(k+" - "+o["name"]+","+o["width"] );
                    }
                }
            }
        }
    }

输出结果

hair - base/hair,1132
h_r - base/h_r,313
0 - base/0,2197
00 - base/00,703
m_light - mouth/m_light,178
..........
..........

你可能感兴趣的:(Unity,C#,json)