Unity网络解析Josn(mvc得小Dome)

效果:



Model层:











Controller层:


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using UnityEngine.Networking;


/// <summary>
/// 程序进入
/// 下载json文档
/// </summary>
public class DownloadData : MonoBehaviour
{
    public static DownloadData instacne;
    string nowDate;


    //标志图片和信息
    All_Sign all_Sign;


    /// <summary>
    /// 运势信息
    /// </summary>
    public List<All_Result> all_Results;


    private void Awake()
    {
        instacne = this;
    }


    private void Start()
    {
        nowDate = DateTime.Now.ToString("yy-MM-dd");//日期得格式 年-月-日
        all_Results = new List<All_Result>();
        StartCoroutine(Download());
    }


    public string SignPath = "http://api.jisuapi.com/astro/all?appkey=d30bee9929c039fc"; //标志下载地址
    IEnumerator Download()
    {
        UnityWebRequest unityWebRequest;
        //WWW www;
        //12个星座下载12次
        for (int i = 1; i <= 12; i++)
        {
            string path = "http://api.jisuapi.com/astro/fortune?astroid=" + i + "&date=20" + nowDate + "&appkey=d30bee9929c039fc";
            //www = new WWW(path);
            unityWebRequest = UnityWebRequest.Get(path);
            yield return unityWebRequest.SendWebRequest();
            //yield return www;
            if (unityWebRequest.error == null)
            {
                //all_Results.Add(JsonMapper.ToObject<All_Result>(www.text));
                all_Results.Add(JsonMapper.ToObject<All_Result>(unityWebRequest.downloadHandler.text));
            }
        }


        //下载标志信息
        unityWebRequest = UnityWebRequest.Get(SignPath);
        yield return unityWebRequest.SendWebRequest();
        //www = new WWW(SignPath);
        //yield return www;
        if (unityWebRequest.error == null)
        {
            all_Sign = JsonMapper.ToObject<All_Sign>(unityWebRequest.downloadHandler.text);
        }


        //www.Dispose();//释放资源
        unityWebRequest.Dispose();
        ShowUI.instance.ShowItem(all_Sign);
    }

}


View层:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;


/// <summary>
/// 显示
/// </summary>
public class ShowUI : MonoBehaviour
{
    public static ShowUI instance;


    Button[] showitem;
    Text[] msgText;


    private void Awake()
    {
        instance = this;
        showitem = GameObject.FindGameObjectWithTag("Group").GetComponentsInChildren<Button>();
        msgText = GameObject.FindGameObjectWithTag("Info").GetComponentsInChildren<Text>();
    }




    /// <summary>
    /// 显示标识
    /// </summary>
    public void ShowItem(All_Sign all_Sign)
    {
        StartCoroutine(Show(all_Sign));
    }




    IEnumerator Show(All_Sign all_Sign)
    {
        //WWW www;
        UnityWebRequest unityWebRequest = null;
        for (int i = 0; i < all_Sign.result.Length; i++)
        {
            showitem[i].GetComponent<OnCilkShow>().Show();
            Text[] texts = showitem[i].GetComponentsInChildren<Text>();
            texts[0].text = all_Sign.result[i].astroname;
            texts[1].text = all_Sign.result[i].date;
            //www = new WWW(all_Sign.result[i].pic);
            unityWebRequest = UnityWebRequestTexture.GetTexture(all_Sign.result[i].pic);
            //yield return www;
            yield return unityWebRequest.SendWebRequest();
            if (unityWebRequest.error == null)
            {
                showitem[i].GetComponentInChildren<RawImage>().texture = (unityWebRequest.downloadHandler as DownloadHandlerTexture).texture;
            }
        }
        unityWebRequest.Dispose();
    }




    public void Show(int index)
    {
        if (msgText == null)
            return;
        msgText[0].text = "今年运势:" + "\n" + DownloadData.instacne.all_Results[index].result.year.ToString();
        msgText[1].text = "今年运势:" + "\n" + DownloadData.instacne.all_Results[index].result.month.ToString();
        msgText[2].text = "今天运势:" + "\n" + DownloadData.instacne.all_Results[index].result.today.ToString();
        msgText[3].text = "今天运势:" + "\n" + DownloadData.instacne.all_Results[index].result.tomorrow.ToString();
    }

}

上面简单得实现了下在往网站上下载json文档并且解析得过程,可能写得不是很好,多多见谅!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值