且构网

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

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

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

您的代码可以正常运行.它不会返回null.您认为它正在返回null,因为所接收的数据中包含\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;
}