读取 json 个对象并找到其中的值
Read json object and find value in it
我有以下 GET 请求:
string url = @"http://api.flexianalysis.com/services/flexianalysisservice.svc/TechnicalAnalysisByCategory?clientid=___&category=forex&key=____";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
//html = reader.ReadToEnd();
}
这是 JSON 的示例:
[{"ID":133739,"TickerID":23,"CategoryID":3,"ClientID":5044,"TickerDateTime":"2017-11-06T12:57:19.267","TickerTitle":"AUD/USD Intraday: key resistance at 0.7670.\r\n","Category":"Forex","TradePairName":"AUD/USD","Ticker":"AUD","TrendType":"THE upside prevails","Status":"Enabled","TrendValue1":"+1","PivotValue":0.767,"OurPreference":"v: short positions below 0.7670 with targets at 0.7635 & 0.7615 in extension.\r\n","AlternateScenario":"o: above 0.7670 look for further upside with 0.7695 & 0.7715 as targets.\r\n","Comments":" as long as 0.7670 is resistance, look for choppy price action with a bearish bias.\r\n","S1":0.7635,"S2":0.7615,"S3":0.7595,"R1":0.767,"R2":0.7695,"R3":0.7715,"Entry":0.0,"Stop":0.0,"T1":0.0,"T2":0.0},{"ID":133738,"TickerID":193,"CategoryID":3,"ClientID":5044,"TickerDateTime":"2017-11-06T12:55:54.33","TickerTitle":"Dollar Index (ICE) Intraday: bullish bias above 94.8000.\r\n","Category":"Forex","TradePairName":"Dollar Index (ICE)","Ticker":"DXA","TrendType":"THE upside prevails","Status":"Enabled","TrendValue1":"+1""PivotValue":94.8,"OurPreference":": long positions above 94.8000 with targets at 95.1500 & 95.3000 in extension.\r\n","AlternateScenario":"below 94.8000 look for further downside with 94.6500 & 94.4500 as targets.\r\n","Comments":": the RSI lacks downward momentum.","S1":94.8,"S2":94.65,"S3":94.45,"R1":95.15,"R2":95.3,"R3":95.45,"Entry":0.0,"Stop":0.0,"T1":0.0,"T2":0.0}]
然后我尝试将其解析为 JSON 并删除开头的 'd':
var json = JObject.Parse(rawJson);
var filter = json["d"];
var fff = filter["ID"];//Get the error here
现在我想读取 ID,但由于某种原因它给出了一个错误,它无法访问子节点。
知道为什么吗?
我认为您需要检查一些假设并尝试一些断点。
查看 API 返回的 JSON 看起来格式不正确,您实际上收到的是 JSON 数组作为字符串:
{"d":"[{\"ID\":133739,\"TickerID\":23,\"CategoryID\":3,...}},
{\"ID\":133740,\"TickerID\":23,\"CategoryID\":3,...}},
[...]}]"}
因此要解析它,您首先需要从 d
参数中获取值,然后将其解析为一个数组:
// Get the response from the server
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
// Pass the response into a stream reader
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
// Grab the JSON response as a string
string rawJson = reader.ReadToEnd();
// Parse the string into a JObject
var json = JObject.Parse(rawJson);
// Get the JToken representing the ASP.NET "d" parameter
var d = json.GetValue("d");
// Parse the string value of the object into a jArray
var jArray = JArray.Parse(d.ToString());
// At this point you can start looking for the items.
}
}
我有以下 GET 请求:
string url = @"http://api.flexianalysis.com/services/flexianalysisservice.svc/TechnicalAnalysisByCategory?clientid=___&category=forex&key=____";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
//html = reader.ReadToEnd();
}
这是 JSON 的示例:
[{"ID":133739,"TickerID":23,"CategoryID":3,"ClientID":5044,"TickerDateTime":"2017-11-06T12:57:19.267","TickerTitle":"AUD/USD Intraday: key resistance at 0.7670.\r\n","Category":"Forex","TradePairName":"AUD/USD","Ticker":"AUD","TrendType":"THE upside prevails","Status":"Enabled","TrendValue1":"+1","PivotValue":0.767,"OurPreference":"v: short positions below 0.7670 with targets at 0.7635 & 0.7615 in extension.\r\n","AlternateScenario":"o: above 0.7670 look for further upside with 0.7695 & 0.7715 as targets.\r\n","Comments":" as long as 0.7670 is resistance, look for choppy price action with a bearish bias.\r\n","S1":0.7635,"S2":0.7615,"S3":0.7595,"R1":0.767,"R2":0.7695,"R3":0.7715,"Entry":0.0,"Stop":0.0,"T1":0.0,"T2":0.0},{"ID":133738,"TickerID":193,"CategoryID":3,"ClientID":5044,"TickerDateTime":"2017-11-06T12:55:54.33","TickerTitle":"Dollar Index (ICE) Intraday: bullish bias above 94.8000.\r\n","Category":"Forex","TradePairName":"Dollar Index (ICE)","Ticker":"DXA","TrendType":"THE upside prevails","Status":"Enabled","TrendValue1":"+1""PivotValue":94.8,"OurPreference":": long positions above 94.8000 with targets at 95.1500 & 95.3000 in extension.\r\n","AlternateScenario":"below 94.8000 look for further downside with 94.6500 & 94.4500 as targets.\r\n","Comments":": the RSI lacks downward momentum.","S1":94.8,"S2":94.65,"S3":94.45,"R1":95.15,"R2":95.3,"R3":95.45,"Entry":0.0,"Stop":0.0,"T1":0.0,"T2":0.0}]
然后我尝试将其解析为 JSON 并删除开头的 'd':
var json = JObject.Parse(rawJson);
var filter = json["d"];
var fff = filter["ID"];//Get the error here
现在我想读取 ID,但由于某种原因它给出了一个错误,它无法访问子节点。 知道为什么吗?
我认为您需要检查一些假设并尝试一些断点。
查看 API 返回的 JSON 看起来格式不正确,您实际上收到的是 JSON 数组作为字符串:
{"d":"[{\"ID\":133739,\"TickerID\":23,\"CategoryID\":3,...}},
{\"ID\":133740,\"TickerID\":23,\"CategoryID\":3,...}},
[...]}]"}
因此要解析它,您首先需要从 d
参数中获取值,然后将其解析为一个数组:
// Get the response from the server
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
// Pass the response into a stream reader
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
// Grab the JSON response as a string
string rawJson = reader.ReadToEnd();
// Parse the string into a JObject
var json = JObject.Parse(rawJson);
// Get the JToken representing the ASP.NET "d" parameter
var d = json.GetValue("d");
// Parse the string value of the object into a jArray
var jArray = JArray.Parse(d.ToString());
// At this point you can start looking for the items.
}
}