我在从this API仅获取价格值时遇到了麻烦。我不需要来自Web URL的所有JSON,我只需要"rate":"3,394.2033"
部分。
API数据:
{
"time": {
"updated": "Feb 6, 2019 22:02:00 UTC",
"updatedISO": "2019-02-06T16:02:00-06:00",
"updateduk": "Feb 6, 2019 at 22:02 GMT"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"bpi": {
"USD": {
"code": "USD",
"rate": "3,394.2033",
"description": "United States Dollar",
"rate_float": 3394.2033
},
"XBT": {
"code": "XBT",
"rate": "1.0000",
"description": "Bitcoin",
"rate_float": 1
}
}
}
我的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class API : MonoBehaviour
{
private string www =
"https://api.coindesk.com/v1/bpi/currentprice/XBT.json";
public Text responseText;
public void Request()
{
WWW request = new WWW(www);
StartCoroutine(OnResponse(request));
}
private IEnumerator OnResponse(WWW req)
{
yield return req;
responseText.text = req.text;
}
}
该脚本允许我提取所有JSON数据,但我只需要"rate":"3,394.2033"
。更具体地说,就是值3,394.2033
。
答案 0 :(得分:1)
使用Json.Net的LINQ-to-JSON API(JTokens),您可以用一行代码来做到这一点:
2019-02-07 10:23:01 DEBUG BasicClientConnectionManager:158 - Get connection for route {}->http://demo0667044.mockable.io
2019-02-07 10:23:01 DEBUG DefaultClientConnectionOperator:174 - Connecting to demo0667044.mockable.io:80
2019-02-07 10:23:02 DEBUG DefaultClientConnection:176 - Connection org.apache.http.impl.conn.DefaultClientConnection@23ceabc1 closed
2019-02-07 10:23:02 DEBUG DefaultClientConnection:160 - Connection org.apache.http.impl.conn.DefaultClientConnection@23ceabc1 shut down
2019-02-07 10:23:02 DEBUG BasicClientConnectionManager:196 - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@5d5eef3d
java.net.ConnectException: Connection refused: connectOutput :
null
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at com.util.TestJavaFile.testMockWS(TestJavaFile.java:128)
at com.util.TestJavaFile.main(TestJavaFile.java:57)
答案 1 :(得分:0)
您可以使用newtonsoft并在此处遵循以下示例:https://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm
public class SearchResult
{
public string Rate { get; set; }
}
JObject rateSearch = JObject.Parse(MyJsonText);
// get JSON result objects into a list
IList<JToken> results = rateSearch ["bpi"]["USD"].Children().ToList();
// serialize JSON results into .NET objects
IList<SearchResult> searchResults = new List<SearchResult>();
foreach (JToken result in results)
{
// JToken.ToObject is a helper method that uses JsonSerializer internally
SearchResult searchResult = result.ToObject<SearchResult>();
searchResults.Add(searchResult);
}
答案 2 :(得分:0)
您还可以使用Unity社区中的旧金SimpleJSON
要在Unity中使用SimpleJSON,只需将SimpleJSON.cs文件复制到资产文件夹内的项目“ plugins”文件夹中。
然后做
point = Point([10, 14, 15, 10, 5])
print(point.weights)
调试说明
如果未找到与给定的键相反的键,则会返回var json = JSON.Parse(req.text);
var yourText = json["bpi"]["USD"]["rate"];
而不是引发异常。
答案 3 :(得分:0)
您可以使用 UnityWebRequest.Get 获取 JSON,然后使用库对其进行反序列化(我推荐 NewtonSoft.Json)。
反序列化 JSON 后,您可以从结果对象中选择要使用的属性。
我整理了一个教程来做这件事,还有更多。