简体   繁体   中英

De-serializing Json with numeric properties in Unity

I am trying to get specific data from CURL but i get whole data Here is the code using

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class getRequest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        string url = "https://blockchain.info/ticker";
        WWW www = new WWW(url);
        StartCoroutine(WaitForRequest(www));

    }
    IEnumerator WaitForRequest(WWW www)
    {
        yield return www;

        // check for errors
        if (www.error == null)
        {            
            Debug.Log("WWW Ok!: " +www.text);
        }
        else
        {
            Debug.Log("WWW Error: " + www.error);
        }
    }
}

Geting OUTPUT like this

WWW Ok!: { "USD" : {"15m" : 1740.01, "last" : 1740.01, "buy" : 1740.01, "sell" : 1744.74, "symbol" : "$"}, "ISK" : {"15m" : 179479.11, "last" : 179479.11, "buy" : 179479.11, "sell" : 179967, "symbol" : "kr"}, "HKD" : {"15m" : 13551.08, "last" : 13551.08, "buy" : 13551.08, "sell" : 13587.92, "symbol" : "$"}, "TWD" : {"15m" : 52292.17, "last" : 52292.17, "buy" : 52292.17, "sell" : 52434.32, "symbol" : "NT$"}, "CHF" : {"15m" : 1733.63, "last" : 1733.63, "buy" : 1733.63, "sell" : 1738.35, "symbol" : "CHF"}, "EUR" : {"15m" : 1584.76, "last" : 1584.76, "buy" : 1584.76, "sell" : 1589.07, "symbol" : "€"}, "DKK" : {"15m" : 11790.2, "last" : 11790.2, "buy" : 11790.2, "sell" : 11822.25, "symbol" : "kr"}, "CLP" : {"15m" : 1161247.87, "last" : 1161247.87, "buy" : 1161247.87, "sell" : 1164404.58, "symbol" : "$"}, "CAD" : {"15m" : 2373.57, "last" : 2373.57, "buy" : 2373.57, "sell" : 2380.02, "symbol" : "$"}, "INR" : {"15m" : 111442.86, "last" : 111442.86, "buy" : 111442.86, "sell" : 111745.8, "symb ol" : "₹"}, "CNY" : {"15m" : 11994.67, "last" : 11994.67, "buy" : 11994.67, "sell" : 12027.28, "symbol" : "¥"}, "THB" : {"15m" : 60079.94, "last" : 60079.94, "buy" : 60079.94, "sell" : 60243.26, "symbol" : "฿"}, "AUD" : {"15m" : 2342.53, "last" : 2342.53, "buy" : 2342.53, "sell" : 2348.9, "symbol" : "$"}, "SGD" : {"15m" : 2434.82, "last" : 2434.82, "buy" : 2434.82, "sell" : 2441.44, "symbol" : "$"}, "KRW" : {"15m" : 1943939.17, "last" : 1943939.17, "buy" : 1943939.17, "sell" : 1949223.53, "symbol" : "₩"}, "JPY" : {"15m" : 197725.17, "last" : 197725.17, "buy" : 197725.17, "sell" : 198262.66, "symbol" : "¥"}, "PLN" : {"15m" : 6655.07, "last" : 6655.07, "buy" : 6655.07, "sell" : 6673.16, "symbol" : "zł"}, "GBP" : {"15m" : 1347.87, "last" : 1347.87, "buy" : 1347.87, "sell" : 1351.53, "symbol" : "£"}, "SEK" : {"15m" : 15356.67, "last" : 15356.67, "buy" : 15356.67, "sell" : 15398.41, "symbol" : "kr"}, "NZD" : {"15m" : 2522.84, "last" : 2522.84, "buy" : 2522.84, "sell" : 2529.69, "s ymbol" : "$"}, "BRL" : {"15m" : 5391.27, "last" : 5391.27, "buy" : 5391.27, "sell" : 5405.92, "symbol" : "R$"}, "RUB" : {"15m" : 98152.22, "last" : 98152.22, "buy" : 98152.22, "sell" : 98419.04, "symbol" : "RUB"} }

But I want output like this USD 1740.01 $ Can any one please help me

Flagged as a duplicate but then re-opened after I realized that the Unity does not support the first value in the Json. Unity's JsonUtility cannot de-serialize the json data when it starts with an integer such as 15m . It can de-serialize other data from that Json but you only want to de-serialize 15m variable.

Download SimpleJSON from Unity's wiki .

With SimpleJSON , this is how you get the USD's 15m value:

var N = JSON.Parse(www.text);
string prize = N["USD"]["15m"].Value;
Debug.Log(prize);

Don't forget to import SimpleJSON with using SimpleJSON;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM