C# nner所以我不知道LINQ是什么。我是否需要知道LINQ才能使用Json.NET?@Nai,是的,事实上强烈建议在使用LINQ之前先学习LINQ。不仅仅是这个案子。作为一名.NET开发人员,这将对您非常有益。您的回答是否假设JSON数据是行/列,而不是自
C# nner所以我不知道LINQ是什么。我是否需要知道LINQ才能使用Json.NET?@Nai,是的,事实上强烈建议在使用LINQ之前先学习LINQ。不仅仅是这个案子。作为一名.NET开发人员,这将对您非常有益。您的回答是否假设JSON数据是行/列,而不是自,c#,json,deserialization,C#,Json,Deserialization,nner所以我不知道LINQ是什么。我是否需要知道LINQ才能使用Json.NET?@Nai,是的,事实上强烈建议在使用LINQ之前先学习LINQ。不仅仅是这个案子。作为一名.NET开发人员,这将对您非常有益。您的回答是否假设JSON数据是行/列,而不是自定义对象?@Tim,这并不重要。我假设它是有效的JSON,这是一个非常安全的假设,因为它的源是Google:-)您可以使用LINQ随意查询它。与查询内存中任何.NET对象的方式相同。这就是林克的力量。数据的格式并不重要。您可以用同样的方式查询它
nner所以我不知道LINQ是什么。我是否需要知道
LINQ
才能使用Json.NET
?@Nai,是的,事实上强烈建议在使用LINQ
之前先学习LINQ
。不仅仅是这个案子。作为一名.NET开发人员,这将对您非常有益。您的回答是否假设JSON数据是行/列,而不是自定义对象?@Tim,这并不重要。我假设它是有效的JSON,这是一个非常安全的假设,因为它的源是Google:-)您可以使用LINQ随意查询它。与查询内存中任何.NET对象的方式相同。这就是林克的力量。数据的格式并不重要。您可以用同样的方式查询它(SQL、XML、内存中的对象、JSON,您可以这样命名…)。我更喜欢使用MS在.NET中已经提供的JavaScriptSerializer。您可以尝试的一件事是将JSON反序列化为类型对象-序列化器。反序列化(jsonContent,typeof(object))
。根据JSON,您将返回一个dictionary对象或类似的对象,然后您可以使用它。我已经更新了我的问题。我觉得我正按照你的回答做,但我似乎没有得到正确的结果。是因为我对类的定义错误吗?如果你能看一下就好了。谢谢。@Cheeso-哇,我需要消化一下。我认为我对正在发生的事情的概念仍然不明确。“反序列化”Json文档时会发生什么?我不明白为什么我需要反序列化我想要的位,然后重新序列化以便能够输出它,因为我已经将收到的响应设置为数据类型string
。关于概念:反序列化json文档(或字符串)是创建一个对象并用从json字符串中提取的值填充其属性。将对象实例序列化为json(反之亦然)是将内存中的(.NET对象)格式转换为json字符串,可供人类查看。您需要序列化GoogleSearchResults对象,以便在打印时将其作为字符串查看。显然,反序列化和(再)序列化是循环的,而真正的程序不会这样做。我在这里做这些只是为了诊断目的:这样你就可以看到反序列化的结果。@Cheeso-我真的很感谢你花时间回答我。因此,让我总结一下我的理解
handleResponse({
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q\u003d{searchTerms}&num\u003d{count?}&start\u003d{startIndex?}&hr\u003d{language?}&safe\u003d{safe?}&cx\u003d{cx?}&cref\u003d{cref?}&sort\u003d{sort?}&alt\u003djson"
},
"queries": {
"nextPage": [
{
"title": "Google Custom Search - lectures",
"totalResults": 9590000,
"searchTerms": "lectures",
"count": 1,
"startIndex": 2,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "017576662512468239146:omuauf_lfve"
}
],
"request": [
{
"title": "Google Custom Search - lectures",
"totalResults": 9590000,
"searchTerms": "lectures",
"count": 1,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "017576662512468239146:omuauf_lfve"
}
]
},
"context": {
"title": "Curriculum",
"facets": [
[
{
"label": "lectures",
"anchor": "Lectures"
}
],
[
{
"label": "assignments",
"anchor": "Assignments"
}
],
[
{
"label": "reference",
"anchor": "Reference"
}
]
]
},
"items": [
{
"kind": "customsearch#result",
"title": "EE364a: Lecture Videos",
"htmlTitle": "EE364a: \u003cb\u003eLecture\u003c/b\u003e Videos",
"link": "http://www.stanford.edu/class/ee364a/videos.html",
"displayLink": "www.stanford.edu",
"snippet": "Apr 7, 2010 ... Course materials. Lecture slides · Lecture videos (2008) · Review sessions. Assignments. Homework · Reading. Exams. Final exam ...",
"htmlSnippet": "Apr 7, 2010 \u003cb\u003e...\u003c/b\u003e Course materials. \u003cb\u003eLecture\u003c/b\u003e slides · \u003cb\u003eLecture\u003c/b\u003e videos (2008) · Review sessions. \u003cbr\u003e Assignments. Homework · Reading. Exams. Final exam \u003cb\u003e...\u003c/b\u003e",
"cacheid": "TxVqFzFZLOsJ"
}
]
}
);
public class GoogleSearchResults
{
public string link { get; set; }
}
public class Program
{
static void Main(string[] args)
{
//input search term
Console.WriteLine("What is your search query?:");
string searchTerm = Console.ReadLine();
//concantenate the strings using + symbol to make it URL friendly for google
string searchTermFormat = searchTerm.Replace(" ", "+");
//create a new instance of Webclient and use DownloadString method from the Webclient class to extract download html
WebClient client = new WebClient();
string Json = client.DownloadString("https://www.googleapis.com/customsearch/v1?key=My Key&cx=My CX&q=" + searchTermFormat);
//create a new instance of JavaScriptSerializer and deserialise the desired content
JavaScriptSerializer js = new JavaScriptSerializer();
GoogleSearchResults results = js.Deserialize<GoogleSearchResults>(Json);
Console.WriteLine(results);
//Console.WriteLine(htmlDoc);
Console.ReadLine();
}
}
// get the id for the uploaded photo
var jss = new JavaScriptSerializer();
var resource = jss.Deserialize<Facebook.Data.Resource>(responseText);
namespace Facebook.Data
{
public class Resource
{
public string id { get; set; }
}
}
{"id":"10150111918987952",
"from":{"name":"Someone",
"id":"782272221"},
"name":"uploaded from Cropper. (at 12\/15\/2010 7:06:41 AM)",
"picture":"http:\/\/photos-f.ak.fbcdn.net\/hphotos-ak-snc4\/hs817.snc4\/69790_101501113333332_782377951_7551951_8193638_s.jpg",
...
namespace Facebook.Data
{
public class Resource
{
public string id { get; set; }
}
public class Person : Resource
{
public string name { get; set; }
}
public class GoogleSearchItem
{
public string kind { get; set; }
public string title { get; set; }
public string link { get; set; }
public string displayLink { get; set; }
// and so on... add more properties here if you want
// to deserialize them
}
public class SourceUrl
{
public string type { get; set; }
public string template { get; set; }
}
public class GoogleSearchResults
{
public string kind { get; set; }
public SourceUrl url { get; set; }
public GoogleSearchItem[] items { get; set; }
// and so on... add more properties here if you want to
// deserialize them
}
// create a new instance of JavaScriptSerializer
JavaScriptSerializer s1 = new JavaScriptSerializer();
// deserialise the received response
GoogleSearchResults results = s1.Deserialize<GoogleSearchResults>(json);
Console.WriteLine(s1.Serialize(results));