且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Unity3D 网站查找在 Unity3D 4.7.2 中返回 nil

更新时间:2022-11-09 23:11:42

您的代码很好并且工作正常.它没有返回空值. 你认为它返回空值是因为你接收的数据中包含 \n\n 使得 json 在下面的几行开始.要实际查看数据,您必须在控制台"选项卡中向下滚动一点或重新调整下方圆圈中水平线的大小.

Your code is fine and is working correctly. It is not returning null. You think it is returning null because the data you are receiving contains \n\n in it making have the json to start in on several lines below. To actually see the data, you have to scroll down a bit in the Console tab or re-size the horizontal line in the circle below.

虽然在 Unity 中使用 UnityWebRequest 更好,但 HttpWebRequest 也应该可以工作.要回答您的其他问题,下载数据后,请使用 JsonUtility.FromJson 取消-将其序列化为一个对象,然后您就可以访问价格.

Although, it is better to use UnityWebRequest in Unity but HttpWebRequest should also work. To answer your other question, once you download the data, use JsonUtility.FromJson to de-serialize it into an Object then you can access the price.

这个函数在 C# 中应该是什么样子:

Here is what that function should look like in C#:

void Start()
{
    StartCoroutine(CheckForPaidApp("http://itunes.apple.com/lookup?id=1218822890")); ;
}


IEnumerator CheckForPaidApp(string uri)
{
    UnityWebRequest uwr = UnityWebRequest.Get(uri);
    yield return uwr.SendWebRequest();

    if (uwr.isHttpError || uwr.isNetworkError)
    {
        Debug.Log("Error While Sending: " + uwr.error);
    }
    else
    {
        string data = uwr.downloadHandler.text;
        Debug.Log("Received: " + uwr.downloadHandler.text);

        //Serialize to Json
        RootObject jsonObj = JsonUtility.FromJson<RootObject>(data);
        List<Result> resultObj = jsonObj.results;

        //Loop over the result and show the price information
        for (int i = 0; i < resultObj.Count; i++)
        {
            double price = resultObj[i].price;
            Debug.Log("Price = \n" + price);

            if (price > 0.0f)
            {
                Debug.Log("Its Paid App\n");
            }
            else
            {
                // show ads here
            }
        }
    }
}

将 json 反序列化为的对象/类:

The Objects/classes to de-serialize json the into:

[Serializable]
public class Result
{
    public List<string> screenshotUrls;
    public List<string> ipadScreenshotUrls;
    public List<object> appletvScreenshotUrls;
    public string artworkUrl512;
    public string artworkUrl60;
    public string artworkUrl100;
    public string artistViewUrl;
    public List<string> supportedDevices;
    public string kind;
    public List<string> features;
    public bool isGameCenterEnabled;
    public List<object> advisories;
    public string fileSizeBytes;
    public List<string> languageCodesISO2A;
    public string trackContentRating;
    public string trackViewUrl;
    public string contentAdvisoryRating;
    public string trackCensoredName;
    public List<string> genreIds;
    public int trackId;
    public string trackName;
    public string primaryGenreName;
    public int primaryGenreId;
    public string currency;
    public string wrapperType;
    public string version;
    public int artistId;
    public string artistName;
    public List<string> genres;
    public double price;
    public string description;
    public string bundleId;
    public string sellerName;
    public bool isVppDeviceBasedLicensingEnabled;
    public DateTime releaseDate;
    public DateTime currentVersionReleaseDate;
    public string minimumOsVersion;
    public string formattedPrice;
}

[Serializable]
public class RootObject
{
    public int resultCount;
    public List<Result> results;
}