Unity C#如何使用JSON实用程序从http服务器读取Json数据

时间:2018-05-15 17:39:26

标签: c# json http unity3d

我真的需要从以下位置读取Json数据: https://api.iextrading.com/1.0/stock/aapl/batch?types=quote,news,chart&range=1m&last=10

不幸的是,因为我是初学者,所以我不太了解有关Json Utility的参考文档。有人可以帮我把数据从Json传递给c#。

Json数据:

{
  "quote": {
    "symbol": "AAPL",
    "companyName": "Apple Inc.",
    "primaryExchange": "Nasdaq Global Select",
    "sector": "Technology",
    "calculationPrice": "close",
    "open": 189.1,
    "openTime": 1526304600392,
    "close": 188.15,
    "closeTime": 1526328000294,
    "high": 189.53,
    "low": 187.86,
    "latestPrice": 188.15,
    "latestSource": "Close",
    "latestTime": "May 14, 2018",
    "latestUpdate": 1526328000294,
    "latestVolume": 15201663,
    "iexRealtimePrice": 188.17,
    "iexRealtimeSize": 100,
    "iexLastUpdated": 1526327999941,
    "delayedPrice": 188.15,
    "delayedPriceTime": 1526328000294,
    "previousClose": 188.59,
    "change": -0.44,
    "changePercent": -0.00233,
    "iexMarketPercent": 0.03066,
    "iexVolume": 466083,
    "avgTotalVolume": 34166845,
    "iexBidPrice": 0,
    "iexBidSize": 0,
    "iexAskPrice": 0,
    "iexAskSize": 0,
    "marketCap": 924783214700,
    "peRatio": 19.34,
    "week52High": 190.37,
    "week52Low": 142.2,
    "ytdChange": 0.10092655711038234
  }
}

我开始编写一些代码来获取json字符串,但我无法解析它。

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

public class DataManager : MonoBehaviour
{

    public string Index_name;
    public string URL;
    public string jsonString;

    // Update is called once per frame
    void Update()
    {
        URL = "https://api.iextrading.com/1.0/stock/" + Index_name + "/batch?types=quote,news,chart&range=1m&last=10";      
    }

    public void Text_Changed(string newIndex)
    {
        Index_name = newIndex;
    }

    public void Request()
    {
        WWW request = new WWW(URL);
        StartCoroutine(OnResponse(request));
    }

    private IEnumerator OnResponse(WWW req)
    {
        yield return req;
        jsonString = req.text;    
    }

0 个答案:

没有答案