首页 > 解决方案 > Unity C# 如何使用 JSON 实用程序从 http 服务器读取 Json 数据

问题描述

我真的需要从以下位置读取 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;    
    }

标签: c#jsonhttpunity3d

解决方案


推荐阅读