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.

2 Answers 2

1

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

contentType: "application/json; charset=utf-8"
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

8 Comments

Is there a way to set it by default to json from the service code?
Although I'm not much help with Android related stuff, I would try calling the web service using Fiddler (a little like this) 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.
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.
fiddler does not see the android query
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.