我正在尝试在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数据.
JS
$(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetProducts",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// How to return data here like a table???
$("#Second").text(msg.d);
//alert(msg.d);
}
});
});
Run Code Online (Sandbox Code Playgroud)
Default.aspx.cs的C#
[WebMethod]
public static string GetProducts()
{
var products = context.GetProducts().ToList();
return What do I have to return ????
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我有以下方法:
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Collections;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
// [System.Web.Script.Services.ScriptService]
public class Tripadvisor : System.Web.Services.WebService {
public Tripadvisor () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HotelAvailability(string api)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(api);
//JsonConvert.SerializeObject(api);
return json ;
}
Run Code Online (Sandbox Code Playgroud)
在这里我设置ResponseFormat属性是json仍然作为XML返回.
我想用json格式使用这个asmx服务有什么想法吗?
我写了一个简单的Web服务,它在JSONText中获取产品列表,它是字符串对象
Web服务代码如下
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
/// <summary>
/// Summary description for JsonWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class JsonWebService : System.Web.Services.WebService
{
public JsonWebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsJson(string prefix)
{
List<Product> products = new List<Product>();
if (prefix.Trim().Equals(string.Empty, StringComparison.OrdinalIgnoreCase))
{
products = ProductFacade.GetAllProducts();
}
else
{
products = …Run Code Online (Sandbox Code Playgroud)