0

I have this code in service

 [WebMethod]        
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public string GetJson(int nNumResults)
 System.Web.Script.Serialization.JavaScriptSerializer serializer = new
        System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows =
          new List<Dictionary<string, object>>();
        Dictionary<string, object> row = null;

    // load dataset from sql query (source not posted here)

        DataSet dset = new DataSet();
        dadapter.Fill(dset);

        if (dset.Tables[0].Rows.Count < 1)
        {
            conn1.Close();
            return null;
        }
        conn1.Close();            

        foreach (DataRow dr in dset.Tables[0].Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dset.Tables[0].Columns)
            {
                row.Add(col.ColumnName.Trim(), dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }

Everything is ok, except that the returned string is json with

<string xmlns="http://www.mysite.com/service"> 

and

</string> 

at the end. If I remove these tags the json can be parsed without any problems.

How can I get the json string without the xml tag at the beginning and end?

I use this request from Android which does not read a valid JSON:

 Map<String, Object> params = new HashMap<String, Object>();
    params.put("nNumQuotes", "100");                                   

    aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject json, AjaxStatus status) {

            try {
        String aJsonString = json.getString("Id");
    } catch (JSONException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }

        }
    });

And also tested from browser with the integrated service test and the same result.

Mario
  • 13,941
  • 20
  • 54
  • 110

2 Answers2

1

Are you including the content-type header in your request?

contentType: "application/json; charset=utf-8"
Brad M
  • 7,857
  • 1
  • 23
  • 40
  • I don't know how. Because in android-query api I don't have such an option and in browser test again there is no option for this. – Mario Dec 19 '13 at 17:49
1

You should add "accept: application/json;" to your header to make sure the server knows you want your data back as JSON, as long as your web service can actually return data in JSON format.

When you test the REST web service in your browser (at least in chrome), it sets the 'accept-type' in the header to this:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

By testing using something like Fiddler, you can change this to something like:

Accept: application/json

That can tell a web service to return data in JSON format. The android code you are using may not be specifying that it wants JSON data. Again, you can use fiddler to see what the android code request looks like.

I just came across this post about a similar issue: ASP.NET JSON web service always return the JSON response wrapped in XML

Community
  • 1
  • 1
Scampbell
  • 1,535
  • 14
  • 24
  • Is there a way to set it by default to json from the service code? – Mario Dec 19 '13 at 17:39
  • Although I'm not much help with Android related stuff, I would try calling the web service using Fiddler ([a little like this](http://csharpdeveloper.wordpress.com/2012/10/24/how-to-post-to-a-rest-api-using-fiddler/)) to know exactly what the web service is returning for various header parameters. Then you can modify your Android request code to generate requests to get what you want from the web service. – Scampbell Dec 19 '13 at 18:01
  • I have called the service from browser (when accessing from localhost the service can be tested in browser) and it still returns the xml header. It should return only the json data. – Mario Dec 19 '13 at 18:03
  • fiddler does not see the android query – Mario Dec 19 '13 at 18:36
  • Fiddler should see incoming and outgoing http requests on port 80 by default. While running fiddler on the web service system, I would make sure that if you call the service through your browser test that the request shows up in fiddler. That way you will know if Fiddler is configured properly. – Scampbell Dec 19 '13 at 18:41
  • yes, the request header have Content-Type: text/xml; charset=utf-8, but I don't know how to change that in android. Is there a way to make asp.net to return json only ? and not xml. – Mario Dec 19 '13 at 18:45
  • No, I believe he issue is that you have to know how to configure your Android request properly.There must be a way to do it somehow, as I'm sure many have in the past. Maybe [this...](http://stackoverflow.com/questions/1945336/how-to-set-content-type-on-httpurlconnection). – Scampbell Dec 19 '13 at 19:00
  • the code I am using is from the AsyncAPI open project, so this is the way they make a request for json – Mario Dec 19 '13 at 19:06