使用 newtonsoft.json 仅从 httprequest 获取一些属性

Get only a few properties from httprequest with newtonsoft.json

我需要解析来自 google 的响应:

https://trends.google.com/trends/api/explore?hl=en-US&tz=-180&req=%7B%22comparisonItem%22:%5B%7B%22keyword%22:%22react%22,%22geo%22:%22%22,%22time%22:%22today+12-m%22%7D%5D,%22category%22:0,%22property%22:%22%22%7D&tz=-180

响应看起来像这样

other properties: ...
widgets: [{token: ""},...]

我使用 RestEase 发送请求。来自 Google 的 RestEase 响应我想映射到我的模型

public class TokenResult
{
    [JsonProperty("widgets[0].token")]
    public string Token { get; set; }
}

restease 尝试解析响应时出现错误。 那么如何才能从第一个小部件中只获得一个令牌呢?

在讨论您的问题要求后,您可以使用 dynamic 从 JSON 字符串中获取 token,而无需使用模型 class。一个工作演示在这里:https://dotnetfiddle.net/bGL8aZ

using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string json=@"{'widgets':[{'request':{'time':'2018-07-09 2019-07-09','resolution':'WEEK','locale':'en-US','comparisonItem':[{'geo':{},'complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'react'}]}}],'requestOptions':{'property':'','backend':'IZG','category':0}},'lineAnnotationText':'Search interest','bullets':[{'text':'react'}],'showLegend':false,'showAverages':false,'helpDialog':{'title':'Interest over time','content':'Numbers represent search interest relative to the highest point on the chart for the given region and time. A value of 100 is the peak popularity for the term. A value of 50 means that the term is half as popular. A score of 0 means there was not enough data for this term.'},'token':'APP6_UEAAAAAXSXKDwW-kwcTrkXhBuCj17xOewtWmROU','id':'TIMESERIES','type':'fe_line_chart','title':'Interest over time','template':'fe','embedTemplate':'fe_embed','version':'1','isLong':true,'isCurated':false},{'request':{'geo':{},'comparisonItem':[{'time':'2018-07-09 2019-07-09','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'react'}]}}],'resolution':'COUNTRY','locale':'en-US','requestOptions':{'property':'','backend':'IZG','category':0}},'geo':'world','resolution':'countries','searchInterestLabel':'Search interest','displayMode':'regions','helpDialog':{'title':'Interest by region','content':'See in which location your term was most popular during the specified time frame. Values are calculated on a scale from 0 to 100, where 100 is the location with the most popularity as a fraction of total searches in that location, a value of 50 indicates a location which is half as popular. A value of 0 indicates a location where there was not enough data for this term. \u003cp\u003e\u003cp\u003e \u003cb\u003eNote:\u003c/b\u003e A higher value means a higher proportion of all queries, not a higher absolute query count. So a tiny country where 80% of the queries are for \'bananas\' will get twice the score of a giant country where only 40% of the queries are for \'bananas\'.','url':'https://support.google.com/trends/answer/4355212'},'color':'PALETTE_COLOR_1','index':0,'bullet':'react','token':'APP6_UEAAAAAXSXKD-rIwyhC21Abw8P2nPfb7AGhxrZ1','id':'GEO_MAP','type':'fe_geo_chart_explore','title':'Interest by region','template':'fe','embedTemplate':'fe_embed','version':'1','isLong':true,'isCurated':false},{'request':{'restriction':{'geo':{},'time':'2018-07-09 2019-07-09','originalTimeRangeForExploreUrl':'today 12-m','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'react'}]}},'keywordType':'ENTITY','metric':['TOP','RISING'],'trendinessSettings':{'compareTime':'2017-07-08 2018-07-08'},'requestOptions':{'property':'','backend':'IZG','category':0},'language':'en'},'helpDialog':{'title':'Related topics','content':'Users searching for your term also searched for these topics. You can view by the following metrics: \u003cp\u003e* \u003cb\u003eTop\u003c/b\u003e - The most popular topics. Scoring is on a relative scale where a value of 100 is the most commonly searched topic and a value of 50 is a topic searched half as often as the most popular term, and so on. \u003cp\u003e* \u003cb\u003eRising\u003c/b\u003e - Related topics with the biggest increase in search frequency since the last time period. Results marked \'Breakout\' had a tremendous increase, probably because these topics are new and had few (if any) prior searches.'},'color':'PALETTE_COLOR_1','keywordName':'react','token':'APP6_UEAAAAAXSXKD9wlRrdtpbUyYXxko216LXZt21ra','id':'RELATED_TOPICS','type':'fe_related_searches','title':'Related topics','template':'fe','embedTemplate':'fe_embed','version':'1','isLong':false,'isCurated':false},{'request':{'restriction':{'geo':{},'time':'2018-07-09 2019-07-09','originalTimeRangeForExploreUrl':'today 12-m','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'react'}]}},'keywordType':'QUERY','metric':['TOP','RISING'],'trendinessSettings':{'compareTime':'2017-07-08 2018-07-08'},'requestOptions':{'property':'','backend':'IZG','category':0},'language':'en'},'helpDialog':{'title':'Related queries','content':'Users searching for your term also searched for these queries. You can sort by the following metrics: \u003cp\u003e* \u003cb\u003eTop\u003c/b\u003e - The most popular search queries. Scoring is on a relative scale where a value of 100 is the most commonly searched query, 50 is a query searched half as often as the most popular query, and so on. \u003cp\u003e* \u003cb\u003eRising\u003c/b\u003e - Queries with the biggest increase in search frequency since the last time period. Results marked \'Breakout\' had a tremendous increase, probably because these queries are new and had few (if any) prior searches.','url':'https://support.google.com/trends/answer/4355000'},'color':'PALETTE_COLOR_1','keywordName':'react','token':'APP6_UEAAAAAXSXKD10s1DrnqESw00VH8rfqAEq4ZjmA','id':'RELATED_QUERIES','type':'fe_related_searches','title':'Related queries','template':'fe','embedTemplate':'fe_embed','version':'1','isLong':false,'isCurated':false}],'keywords':[{'keyword':'react','name':'react','type':'Search term'}],'timeRanges':['Past 12 months'],'examples':[],'shareText':'Explore search interest for react by time, location and popularity on Google Trends','shouldShowMultiHeatMapMessage':false}";

        dynamic data = JsonConvert.DeserializeObject(json);

        foreach(var result in data.widgets)
        {    
            Console.WriteLine(result.token);
        }
    }
}

输出:

APP6_UEAAAAAXSXKDwW-kwcTrkXhBuCj17xOewtWmROU
APP6_UEAAAAAXSXKD-rIwyhC21Abw8P2nPfb7AGhxrZ1
APP6_UEAAAAAXSXKD9wlRrdtpbUyYXxko216LXZt21ra
APP6_UEAAAAAXSXKD10s1DrnqESw00VH8rfqAEq4ZjmA

您需要处理文本文件中实际 JSON 字符串之前的额外字符,才能使其正常工作。

使用 .SelectTokens(path) 的替代解决方案:

var json = "{\n\t\"widgets\": [{\n\t\t\t\"request\": {\n\t\t\t\t\"time\": \"2018-07-10 2019-07-10\",\n\t\t\t\t\"resolution\": \"WEEK\",\n\t\t\t\t\"locale\": \"en-US\",\n\t\t\t\t\"comparisonItem\": [{\n\t\t\t\t\t\t\"geo\": {},\n\t\t\t\t\t\t\"complexKeywordsRestriction\": {\n\t\t\t\t\t\t\t\"keyword\": [{\n\t\t\t\t\t\t\t\t\t\"type\": \"BROAD\",\n\t\t\t\t\t\t\t\t\t\"value\": \"react\"\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\t\"requestOptions\": {\n\t\t\t\t\t\"property\": \"\",\n\t\t\t\t\t\"backend\": \"IZG\",\n\t\t\t\t\t\"category\": 0\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"lineAnnotationText\": \"Search interest\",\n\t\t\t\"bullets\": [{\n\t\t\t\t\t\"text\": \"react\"\n\t\t\t\t}\n\t\t\t],\n\t\t\t\"showLegend\": false,\n\t\t\t\"showAverages\": false,\n\t\t\t\"helpDialog\": {\n\t\t\t\t\"title\": \"Interest over time\",\n\t\t\t\t\"content\": \"Numbers represent search interest relative to the highest point on the chart for the given region and time. A value of 100 is the peak popularity for the term. A value of 50 means that the term is half as popular. A score of 0 means there was not enough data for this term.\"\n\t\t\t},\n\t\t\t\"token\": \"APP6_UEAAAAAXSbu2jqA25pSUFsKBYxQA0kFsKdcLCgU\",\n\t\t\t\"id\": \"TIMESERIES\",\n\t\t\t\"type\": \"fe_line_chart\",\n\t\t\t\"title\": \"Interest over time\",\n\t\t\t\"template\": \"fe\",\n\t\t\t\"embedTemplate\": \"fe_embed\",\n\t\t\t\"version\": \"1\",\n\t\t\t\"isLong\": true,\n\t\t\t\"isCurated\": false\n\t\t}, {\n\t\t\t\"request\": {\n\t\t\t\t\"geo\": {},\n\t\t\t\t\"comparisonItem\": [{\n\t\t\t\t\t\t\"time\": \"2018-07-10 2019-07-10\",\n\t\t\t\t\t\t\"complexKeywordsRestriction\": {\n\t\t\t\t\t\t\t\"keyword\": [{\n\t\t\t\t\t\t\t\t\t\"type\": \"BROAD\",\n\t\t\t\t\t\t\t\t\t\"value\": \"react\"\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\t\"resolution\": \"COUNTRY\",\n\t\t\t\t\"locale\": \"en-US\",\n\t\t\t\t\"requestOptions\": {\n\t\t\t\t\t\"property\": \"\",\n\t\t\t\t\t\"backend\": \"IZG\",\n\t\t\t\t\t\"category\": 0\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"geo\": \"world\",\n\t\t\t\"resolution\": \"countries\",\n\t\t\t\"searchInterestLabel\": \"Search interest\",\n\t\t\t\"displayMode\": \"regions\",\n\t\t\t\"helpDialog\": {\n\t\t\t\t\"title\": \"Interest by region\",\n\t\t\t\t\"content\": \"See in which location your term was most popular during the specified time frame. Values are calculated on a scale from 0 to 100, where 100 is the location with the most popularity as a fraction of total searches in that location, a value of 50 indicates a location which is half as popular. A value of 0 indicates a location where there was not enough data for this term. \u003cp\u003e\u003cp\u003e \u003cb\u003eNote:\u003c/b\u003e A higher value means a higher proportion of all queries, not a higher absolute query count. So a tiny country where 80% of the queries are for \\"bananas\\" will get twice the score of a giant country where only 40% of the queries are for \\"bananas\\".\",\n\t\t\t\t\"url\": \"https://support.google.com/trends/answer/4355212\"\n\t\t\t},\n\t\t\t\"color\": \"PALETTE_COLOR_1\",\n\t\t\t\"index\": 0,\n\t\t\t\"bullet\": \"react\",\n\t\t\t\"token\": \"APP6_UEAAAAAXSbu2q_7M5NSV_6S_n66X17hnN3geL_X\",\n\t\t\t\"id\": \"GEO_MAP\",\n\t\t\t\"type\": \"fe_geo_chart_explore\",\n\t\t\t\"title\": \"Interest by region\",\n\t\t\t\"template\": \"fe\",\n\t\t\t\"embedTemplate\": \"fe_embed\",\n\t\t\t\"version\": \"1\",\n\t\t\t\"isLong\": true,\n\t\t\t\"isCurated\": false\n\t\t}, {\n\t\t\t\"request\": {\n\t\t\t\t\"restriction\": {\n\t\t\t\t\t\"geo\": {},\n\t\t\t\t\t\"time\": \"2018-07-10 2019-07-10\",\n\t\t\t\t\t\"originalTimeRangeForExploreUrl\": \"today 12-m\",\n\t\t\t\t\t\"complexKeywordsRestriction\": {\n\t\t\t\t\t\t\"keyword\": [{\n\t\t\t\t\t\t\t\t\"type\": \"BROAD\",\n\t\t\t\t\t\t\t\t\"value\": \"react\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t\"keywordType\": \"ENTITY\",\n\t\t\t\t\"metric\": [\"TOP\", \"RISING\"],\n\t\t\t\t\"trendinessSettings\": {\n\t\t\t\t\t\"compareTime\": \"2017-07-09 2018-07-09\"\n\t\t\t\t},\n\t\t\t\t\"requestOptions\": {\n\t\t\t\t\t\"property\": \"\",\n\t\t\t\t\t\"backend\": \"IZG\",\n\t\t\t\t\t\"category\": 0\n\t\t\t\t},\n\t\t\t\t\"language\": \"en\"\n\t\t\t},\n\t\t\t\"helpDialog\": {\n\t\t\t\t\"title\": \"Related topics\",\n\t\t\t\t\"content\": \"Users searching for your term also searched for these topics. You can view by the following metrics: \u003cp\u003e* \u003cb\u003eTop\u003c/b\u003e - The most popular topics. Scoring is on a relative scale where a value of 100 is the most commonly searched topic and a value of 50 is a topic searched half as often as the most popular term, and so on. \u003cp\u003e* \u003cb\u003eRising\u003c/b\u003e - Related topics with the biggest increase in search frequency since the last time period. Results marked \\"Breakout\\" had a tremendous increase, probably because these topics are new and had few (if any) prior searches.\"\n\t\t\t},\n\t\t\t\"color\": \"PALETTE_COLOR_1\",\n\t\t\t\"keywordName\": \"react\",\n\t\t\t\"token\": \"APP6_UEAAAAAXSbu2j6t0UwOOyihkflYRxWkIyxgSgNY\",\n\t\t\t\"id\": \"RELATED_TOPICS\",\n\t\t\t\"type\": \"fe_related_searches\",\n\t\t\t\"title\": \"Related topics\",\n\t\t\t\"template\": \"fe\",\n\t\t\t\"embedTemplate\": \"fe_embed\",\n\t\t\t\"version\": \"1\",\n\t\t\t\"isLong\": false,\n\t\t\t\"isCurated\": false\n\t\t}, {\n\t\t\t\"request\": {\n\t\t\t\t\"restriction\": {\n\t\t\t\t\t\"geo\": {},\n\t\t\t\t\t\"time\": \"2018-07-10 2019-07-10\",\n\t\t\t\t\t\"originalTimeRangeForExploreUrl\": \"today 12-m\",\n\t\t\t\t\t\"complexKeywordsRestriction\": {\n\t\t\t\t\t\t\"keyword\": [{\n\t\t\t\t\t\t\t\t\"type\": \"BROAD\",\n\t\t\t\t\t\t\t\t\"value\": \"react\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t\"keywordType\": \"QUERY\",\n\t\t\t\t\"metric\": [\"TOP\", \"RISING\"],\n\t\t\t\t\"trendinessSettings\": {\n\t\t\t\t\t\"compareTime\": \"2017-07-09 2018-07-09\"\n\t\t\t\t},\n\t\t\t\t\"requestOptions\": {\n\t\t\t\t\t\"property\": \"\",\n\t\t\t\t\t\"backend\": \"IZG\",\n\t\t\t\t\t\"category\": 0\n\t\t\t\t},\n\t\t\t\t\"language\": \"en\"\n\t\t\t},\n\t\t\t\"helpDialog\": {\n\t\t\t\t\"title\": \"Related queries\",\n\t\t\t\t\"content\": \"Users searching for your term also searched for these queries. You can sort by the following metrics: \u003cp\u003e* \u003cb\u003eTop\u003c/b\u003e - The most popular search queries. Scoring is on a relative scale where a value of 100 is the most commonly searched query, 50 is a query searched half as often as the most popular query, and so on. \u003cp\u003e* \u003cb\u003eRising\u003c/b\u003e - Queries with the biggest increase in search frequency since the last time period. Results marked \\"Breakout\\" had a tremendous increase, probably because these queries are new and had few (if any) prior searches.\",\n\t\t\t\t\"url\": \"https://support.google.com/trends/answer/4355000\"\n\t\t\t},\n\t\t\t\"color\": \"PALETTE_COLOR_1\",\n\t\t\t\"keywordName\": \"react\",\n\t\t\t\"token\": \"APP6_UEAAAAAXSbu2mSFz-N3e9aT0TUMZbJ-YOtU2Qjp\",\n\t\t\t\"id\": \"RELATED_QUERIES\",\n\t\t\t\"type\": \"fe_related_searches\",\n\t\t\t\"title\": \"Related queries\",\n\t\t\t\"template\": \"fe\",\n\t\t\t\"embedTemplate\": \"fe_embed\",\n\t\t\t\"version\": \"1\",\n\t\t\t\"isLong\": false,\n\t\t\t\"isCurated\": false\n\t\t}\n\t],\n\t\"keywords\": [{\n\t\t\t\"keyword\": \"react\",\n\t\t\t\"name\": \"react\",\n\t\t\t\"type\": \"Search term\"\n\t\t}\n\t],\n\t\"timeRanges\": [\"Past 12 months\"],\n\t\"examples\": [],\n\t\"shareText\": \"Explore search interest for react by time, location and popularity on Google Trends\",\n\t\"shouldShowMultiHeatMapMessage\": false\n}\n";

var obj = JToken.Parse(json);

var tokens = obj.SelectTokens("widgets[*].token");