1. 程式人生 > >Unity中從網路獲取JSON

Unity中從網路獲取JSON

參考地址

http://www.360doc.com/content/13/0117/11/10941785_260686840.shtml

將LitJson.dll放在assert下的plugins裡然後如下操作

    void Start()
    {

        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            print("Network disconnected;");

        }
        else
        {
            StartCoroutine(GetPhotos());
        }
        
    }

    // Update is called once per frame  
    IEnumerator GetPhotos()
    {
        WWWForm form = new WWWForm();
        form.AddField("key", "896d6bbceafa4cd4b9cf22cee7a0e7cc");
        form.AddField("info","天氣");
        WWW w = new WWW("http://www.tuling123.com/openapi/api", form);
        while (!w.isDone) { yield return new WaitForEndOfFrame(); }
        Debug.Log(w.text);
        JsonData jd = JsonMapper.ToObject(w.text);
        Debug.Log("text=" + jd["text"]);
        

    }