简体   繁体   English

ASP.NET Service使用xml标头返回JSON数据

[英]ASP.NET Service returns JSON data with xml header

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 一切都很好,除了返回的字符串是json

<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. 如果我删除这些标签,可以解析json没有任何问题。

How can I get the json string without the xml tag at the beginning and end? 如何在开头和结尾处获取没有xml标记的json字符串?

I use this request from Android which does not read a valid JSON: 我使用Android的这个请求,它不读取有效的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. 并且还通过集成服务测试和相同的结果从浏览器进行了测试。

您是否在请求中包含content-type标题?

contentType: "application/json; charset=utf-8"

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. 您应该在标头中添加“ accept: application/json; ”,以确保服务器知道您希望将数据恢复为JSON,只要您的Web服务实际上可以以JSON格式返回数据。

When you test the REST web service in your browser (at least in chrome), it sets the 'accept-type' in the header to this: 当您在浏览器中测试REST Web服务时(至少在chrome中),它会在标头中将“accept-type”设置为:

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: 通过使用类似Fiddler的测试,您可以将其更改为:

Accept: application/json

That can tell a web service to return data in JSON format. 这可以告诉Web服务以JSON格式返回数据。 The android code you are using may not be specifying that it wants JSON data. 您正在使用的Android代码可能没有指定它想要JSON数据。 Again, you can use fiddler to see what the android code request looks like. 同样,你可以使用fiddler来查看android代码请求的样子。

I just came across this post about a similar issue: ASP.NET JSON web service always return the JSON response wrapped in XML 我刚刚看到这篇关于类似问题的帖子: ASP.NET JSON Web服务总是返回用XML包装的JSON响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM