我正在尝试从Google AJAX Search API解析一些JSON数据.我有这个URL,我想将其分解,以便显示结果.我目前已经编写了这段代码,但我对于下一步该做什么感到非常遗憾,尽管有很多例子都有简化的JSON字符串.
作为C#和.NET的新手,我一直努力为我的ASP.NET页面获得真正的文本输出,所以我一直建议尝试JSON.NET.任何人都可以指出我正确的方向只是简单地编写一些代码,这些代码将从Google AJAX Search API中获取JSON并将其打印到屏幕上吗?
编辑:全部固定!所有结果都很好.再次感谢Dreas Grech!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GoogleSearchResults g1 = new GoogleSearchResults();
const string json = @"{""responseData"": {""results"":[{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.cheese.com/"",""url"":""http://www.cheese.com/"",""visibleUrl"":""www.cheese.com"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:bkg1gwNt8u4J:www.cheese.com"",""title"":""\u003cb\u003eCHEESE\u003c/b\u003e.COM - All about \u003cb\u003echeese\u003c/b\u003e!."",""titleNoFormatting"":""CHEESE.COM - All about cheese!."",""content"":""\u003cb\u003eCheese\u003c/b\u003e - everything you want to know …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Visual Studio 2011上为Windows 8构建Metro应用程序.虽然我正在尝试这样做,但我遇到了一些关于如何在JSON没有JSON.NET库的情况下解析的问题(它还不支持metro应用程序) .
无论如何,我想解析一下:
{
"name":"Prince Charming",
"artist":"Metallica",
"genre":"Rock and Metal",
"album":"Reload",
"album_image":"http:\/\/up203.siz.co.il\/up2\/u2zzzw4mjayz.png",
"link":"http:\/\/f2h.co.il\/7779182246886"
}
Run Code Online (Sandbox Code Playgroud) 我们经常希望使用JSON来实现人类可读性.因此,通常要求在Go中,在.NET中,在Python中,在Java中按字母顺序(或字母数字)对JSON键进行排序,...
但是如何在Swift中按字母顺序输出带有JSON键的JSON?
PrettyPrinted输出很简单:
JSONSerialization.writeJSONObject(jsonObject, to: outputStream, options: [.prettyPrinted], error: nil)
Run Code Online (Sandbox Code Playgroud)
然而,按键不按字母顺序排列以供人类阅读.它们很可能是由NSDictionary.keyEnumerator()给出的顺序.但遗憾的是,我们不能在Swift中继承Dictionary,NSDictionary或CFDictionary,因此我们无法覆盖键顺序的行为.
[编辑:实际上,我们可以继承NSDictionary,请参阅下面的一个答案]