ASP.NET JSON web service response format

I wrote one simple web service that gets a list of products in a JSONText, which is a string object

Web service code below

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; } } 

now it gives me the answer as below:

{"d": "[{\" ProductID \ ": 1, \" ProductName \ ": \" Product 1 \ "}, {\" ProductID \ ": 2, \ ProductName \": \ "Product 2 \" }, {\ "ProductID \": 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 \": \ "Product 9 \ "}, {\" ProductID \ ": 10, \ ProductName \": \ "Product 10 \"}] "}

But I am looking below

[{"ProductID": 1, "ProductName": "Product 1"}, {"ProductID": 2, "ProductName": "Product 2"}, {"ProductID": 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",: "Product 9 "}, {" ProductID ": 10," ProductName ":" Product 10 "}]

can anyone tell me what is the real problem

thank

+5
json service response
Aug 6 '09 at 12:49
source share
4 answers

First, there were changes with ASP.NET 3.5 for security reasons. Microsoft added the answer "d". Below is a link from Dave Ward to Encosia that talks about what you see: The gap between ASP.NET AJAX versions . He has a few posts that talk about this, which may help you in further processing JSON and ASP.NET

+7
Aug 6 '09 at 13:14
source share

Actually, if you just delete

 [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

from the method, and you return the jsonString that you serialized using the JavaScriptSerializer, you will get the exact result you were looking for.

0
May 27 '10 at 22:03
source share

Note that u has double quotes next to ur array in your answer. So u returns the json format, not the json object from the ur web method. The Json format is a string. Therefore, you need to use the json.parse () function in order to parse the json string for the json object. If you do not want to use parsing, you need to remove serialization in the ur web method. This way you get the json object.

0
Mar 13 '15 at 8:27
source share

in the .net web service

  [WebMethod] public string Android_DDD(string KullaniciKey, string Durum, string PersonelKey) { return EU.EncodeToBase64("{\"Status\":\"OK\",\"R\":[{\"ImzaTipi\":\"Paraf\", \"Personel\":\"Ali Veli üğiΕŸΓ§ΓΆΔ±ΓœΔžΔ°ΕžΓ‡Γ–I\", \"ImzaDurumTipi\":\"Tamam\", \"TamamTar\":\"1.1.2003 11:21\"},{\"ImzaTipi\":\"Δ°mza\", \"Personel\":\"Ali Ak\", \"ImzaDurumTipi\":\"Tamam\", \"TamamTar\":\"2.2.2003 11:21\"}]}"); } static public string EncodeToBase64(string toEncode) { UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = encoding.GetBytes(toEncode); string returnValue = System.Convert.ToBase64String(bytes); return returnValue; } 

in android

  private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private void LoadJsonDataFromASPNET() { try { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPostRequest = new HttpPost(this.WSURL + "/WS.asmx/Android_DDD"); JSONObject jsonObjSend = new JSONObject(); jsonObjSend.put("KullaniciKey", "value_1"); jsonObjSend.put("Durum", "value_2"); jsonObjSend.put("PersonelKey", "value_3"); StringEntity se = new StringEntity(jsonObjSend.toString()); httpPostRequest.setEntity(se); httpPostRequest.setHeader("Accept", "application/json"); httpPostRequest.setHeader("Content-type", "application/json"); // httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String resultString = convertStreamToString(instream); instream.close(); resultString = resultString.substring(6, resultString.length()-3); resultString = new String(android.util.Base64.decode(resultString, 0), "UTF-8"); JSONObject object = new JSONObject(resultString); String oDurum = object.getString("Status"); if (oDurum.equals("OK")) { JSONArray jsonArray = new JSONArray(object.getString("R")); if (jsonArray.length() > 0) { for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String ImzaTipi = jsonObject.getString("ImzaTipi"); String Personel = jsonObject.getString("Personel"); String ImzaDurumTipi = jsonObject.getString("ImzaDurumTipi"); String TamamTar = jsonObject.getString("TamamTar"); Toast.makeText(getApplicationContext(), "ImzaTipi:" + ImzaTipi + " Personel:" + Personel + " ImzaDurumTipi:" + ImzaDurumTipi + " TamamTar:" + TamamTar, Toast.LENGTH_LONG).show(); } } } } } catch (Exception e) { e.printStackTrace(); } } 
-one
Oct 07 '13 at 20:06 on
source share



All Articles