ho scritto un servizio web semplice, che ottiene l'elenco dei prodotti in JSONText che è oggetto stringa di codice ServizioASP.NET JSON Web Service Response formato
Web è al di sotto
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 = ProductFacade.GetProducts(prefix);
}
//yourobject is your actula object (may be collection) you want to serialize to json
DataContractJsonSerializer serializer = new DataContractJsonSerializer(products.GetType());
//create a memory stream
MemoryStream ms = new MemoryStream();
//serialize the object to memory stream
serializer.WriteObject(ms, products);
//convert the serizlized object to string
string jsonString = Encoding.Default.GetString(ms.ToArray());
//close the memory stream
ms.Close();
return jsonString;
}
}
ora dammi resoponse come sotto:
{"d": "[{\" ProductID \ ": 1, \" ProductName \ ": \" Prodotto 1 \ "}, {\" IDProdotto ": 2, \" NomeProdotto \ ": \" Prodotto 2 \ "}, {\" IDProdotto ": 3, \" NomeProdotto \ ": \" Prodotto 3 \ "}, {\" IDProdotto ": 4, \" NomeProdotto \ ": \ "Prodotto 4 \"}, {\ "ProductID \": 5, \ "ProductName \": \ "Prodotto 5 \"}, {\ "IDProdotto": 6, \ "NomeProdotto \": \ "Prodotto 6 \"}, {\ "ID Prodotto \ ": 7, \" NomeProdotto \ ": \" Prodotto 7 \ "}, {\" IDProdotto ": 8, \" NomeProdotto \ ": \" Prodotto 8 \ "}, {\" IDProdotto ": 9 , \ "ProductName \": \ "Product 9 \"}, {\ "ProductID \": 10, \ "ProductName \": \ "Prodotto 10 \"}] "}
Ma sto cercando put below put
[{"ProductID": 1, "ProductName": "Prodotto 1"}, {"ID prodotto": 2, "ProductName": "Prodotto 2"}, {"ID prodotto": 3, "ProductName": "Product 3"}, {"ProductID": 4, "ProductName": "Product 4"}, {"ProductID": 5, "ProductName": "Product 5"}, {"ProductID": 6 , "ProductName": "Product 6"}, {"ProductID": 7, "ProductName": "Product 7"}, {"ProductID": 8, "ProductName": "Product 8"}, {"ProductID": 9, "ProductName": "P roduct 9 "}, {" ProductID ": 10," ProductName ":" 10" Prodotto}]
uno può dirmi che cosa è vero problema
Grazie
Grazie ewrankin per la gentile risposta ma il mio problema è che devo andare con asp.net 2.0 framework, quindi per favore mi suggerisci come posso ottenere questo. per favore se avete qualche altra opzione che per favore dimmi – Hiscal
Mi dispiace, credo di non capire il tuo commento. Vuoi andare su ASP.NET 2.0? Perché in base alla risposta JSON che stai ricevendo stai utilizzando ASP.NET 3.5 a causa della "d" aggiuntiva che viene aggiunta. O stai chiedendo come lavorare con la "d" nella risposta che stai ricevendo. – ewrankin
Ho lavorato con il tuo suggerimento ma continua a darmi lo stesso risultato con '\' indesiderato e non mi dà output come {"d": "[{" ProductID ": 1," ProductName ":" Prodotto 1 " }, {"ProductID": 2, "ProductName": "Product 2"}, {"ProductID": 3, "ProductName": "Product 3"}, {"ProductID": 4, "ProductName": "Prodotto 4 "}, {" ProductID ": 5," ProductName ":" Product 5 "}, {" ProductID ": 6," ProductName ":" Product 6 "}, {" ProductID ": 7," ProductName ":" Prodotto 7 "}, {" ProductID ": 8," ProductName ":" Prodotto 8 "}, {" ID prodotto ": 9," ProductName ":" Prodotto 9 "}, {" ID prodotto ": 10," Nome prodotto ":" Prodotto 10 "}]"} – Hiscal