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.