【发布时间】:2020-04-24 13:43:20
【问题描述】:
C# 代码:
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace BTC_Changex
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
double bitcoin_price_dollar = bitcoin_price_method();
}
public static double bitcoin_price_method()
{
double bitcoin_price = 8500;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd");
req.Method = "GET";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
req.ContentType = "text/html; charset=utf-8";
req.Referer = "";
req.KeepAlive = true;
req.Timeout = 25000;
req.AllowAutoRedirect = true;
CookieContainer cookieJar1 = new CookieContainer();
req.CookieContainer = cookieJar1;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
foreach (Cookie cookie in res.Cookies)
{
cookieJar1.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), "/", cookie.Domain));
}
Stream Stream = res.GetResponseStream();
StreamReader reader = new StreamReader(Stream);
string reader_str = reader.ReadToEnd();
var obj = JObject.Parse(reader_str);
string bitcoin_price_str = ((string)obj["0"]["current_price"]).Trim().Replace(",", "");
bitcoin_price = double.Parse(bitcoin_price_str);
reader.Close();
Stream.Close();
res.Close();
}
catch (Exception ex)
{
}
return bitcoin_price;
}
}
}
我在这一行有错误:var obj = JObject.Parse(reader_str);
错误信息:
从 JsonReader 读取 JObject 时出错。当前的 JsonReader 项目不是 一个对象:StartArray。路径'',第 1 行,位置 1。
问题是什么?我该如何解决?
编辑:
这是 reader_str :https://pastebin.com/fyv7GPVH
【问题讨论】:
-
你能分享一些关于 reader_str 的信息吗?
-
这里是 reader_str :pastebin.com/fyv7GPVH
-
您也可以在 Firefox 中打开该网址 - 您会看到该 json 没有问题。怎么了?
-
结果没问题,我认为它的原因是您试图将数组解析为对象。
-
只要找到我提到的使用作业或对象两种方式的解决方案。希望对你有帮助