ASP.NET JSON Web Service Response format

前端 未结 4 1608
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 17:37

I have written one simple web service which get product list in JSONText which is string object

Web Service code is below

using System;
using Syste         


        
相关标签:
4条回答
  • 2020-12-11 18:22

    First there was a change with ASP.NET 3.5 for security reasons Microsoft added the "d" to the response. Below is a link from Dave Ward at the Encosia that talks about what your seeing: A breaking change between versions of ASP.NET AJAX. He has several posts that talks about this that can help you further with processing JSON and ASP.NET

    0 讨论(0)
  • 2020-12-11 18:26

    Actually, if you just remove the

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    

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

    0 讨论(0)
  • 2020-12-11 18:39

    Notice that u have double quotes beside ur array in your response.In this way u return json format not json object from ur web method.Json format is a string.Therefore u have to use json.parse() function in order to parse json string to json object.If u dont want to use parse fuction,u have to remove serialize in ur web method.Thus u get a json object.

    0 讨论(0)
  • 2020-12-11 18:40

    in .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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题