Unity 解析Json

 原理很简单 就是通过http请求返回的json 进行序列化 解析

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using System;
using System.Linq;
using System.Text;
using System.Threading;
using LitJson;
using System.IO;//
// using Newtonsoft;
// using Newtonsoft.Json.Linq;
// using Newtonsoft.Json;
[Serializable]
public class getTheLevel
{
    public string code;
    public data data;
    public string err;
}
[Serializable]
public class data
{
    public float GameMode;
    public float GameSpeed;
    public string Lv;
    public double time;
    public static int BoxNum;
    public string RequestId;
    public override string ToString()
    {
        return string.Format("GameMode:[{0}],GameSpeed:[{1}],Lv:[{2}],times:[{3}],BoxNum:[{4}],RequestId:[{5}]",
        GameMode, GameSpeed, Lv, time, BoxNum, RequestId);
    }

    public boxLists[] BoxList;
}
[Serializable]
public class boxLists
{
    public string BoxName;
    public float Score;
    public float Distance;
    public string Uid;
    public float IntermediateBonus;
    public float Zoom;

    public override string ToString()
    {
        return string.Format("BoxName:[{0}],Score:[{1}],Distance:[{2}],Uid:[{3}],IntermediateBonus:[{4}],Zoom:[{5}]",
        BoxName, Score, Distance, Uid, IntermediateBonus, Zoom);
    }
}
public class HttpPost : MonoBehaviour
{
    public static string authorization = "";
    string postUrl = "http://.........";


    private string url = "http:/.....";
    private string jsonData = null;

    void Start()
    {
        WWWForm form = new WWWForm();

        form.AddField("phone", "1760...0");  //需要提交的数据也由后端提供

        form.AddField("password", "123456");

        StartCoroutine(SendPost(postUrl, form));
    }



    IEnumerator SendPost(string _url, WWWForm _wForm)

    {

        WWW postData = new WWW(_url, _wForm);

        yield return postData;

        if (postData.error != null)
        {

            Debug.Log(postData.error);
            Debug.Log("null");

        }
        else
        {

            string str = postData.text.ToString();

            JsonData data = JsonMapper.ToObject(str);

            print(data["data"]["jwt_token"]);

            print(data["data"]["username"]); //由后端提供json数据
            authorization = data["data"]["jwt_token"].ToString();
            print(authorization);
            authorization = "Bearer " + authorization;

            StartCoroutine(CallHttpGet(url, authorization, jsonData));
        }
    }
    IEnumerator CallHttpGet(string url, string auth, string json)
    {
        Dictionary<string, string> header = new Dictionary<string, string>();
        byte[] postBytes;
        //header.Add("game_type", "game_lv");//添加header
        if (auth != null)
        {
            header.Add("Authorization", auth);
            //header.Add("game_type", "game_lv");
        }
        if (json != null)
        {
            //POST
            //将数据转换成json数据流
            postBytes = System.Text.Encoding.UTF8.GetBytes(json);
        }
        else
        {
            //GET
            //此案例不需要json数据所以为null
            postBytes = null;
        }
        WWW www = new WWW(url, postBytes, header);
        yield return www;

        if (www.error != null)
        {
            Debug.Log("error is :" + www.error);
        }
        else
        {
            var str = www.text;
            Debug.Log(str);
            var getTheLevel = JsonUtility.FromJson<getTheLevel>(str);
            //new getTheLevel()
            Debug.Log(getTheLevel.code);
            Debug.Log(getTheLevel.data);
            Debug.Log(getTheLevel.err);
            string jsonData = JsonUtility.ToJson(getTheLevel.data);
            var data = JsonUtility.FromJson<data>(jsonData);
            Debug.Log(getTheLevel.data.ToString());
            foreach (var boxList in data.BoxList)
            {
                Debug.Log(boxList);
            }

        }
        yield return null;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36848370/article/details/103145079