0

I try to make a JSON web service with use asp.net 3.5 and the result returning in xml like this :

<string xmlns="http://.../">
[{"HatId":9,"HatAdi":"SARISU - GÜZELOBA","LevhaAciklama":"KL08"}]
</string>

I used that LINK example and I realized that sample also returning as a xml.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HatlarJSON(String HatAdi)
    {

        Hatlar[] allRecords = null;
        string sql = "SELECT * FROM Table";
        SqlConnection con = new SqlConnection("Data Source="ip";Initial Catalog="";User Id="";Password="";Integrated Security=False");
        con.Open();
        using (var command = new SqlCommand(sql, con))
        {
            using (var reader = command.ExecuteReader())
            {
                var list = new List<Hatlar>();
                while (reader.Read())
                    list.Add(new Hatlar { HatId = reader.GetInt32(0), HatAdi = reader.GetString(1), LevhaAciklama = reader.GetString(2) });
                allRecords = list.ToArray();
            }
        }


        return new JavaScriptSerializer().Serialize(allRecords);


    }

How can I return the solution with no xml ?

Anyone have an idea ?

Harold W
  • 77
  • 1
  • 10

1 Answers1

1
[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void HatlarJSON(String HatAdi)
    {

        Hatlar[] allRecords = null;
        string sql = "SELECT * FROM Table";
        SqlConnection con = new SqlConnection("Data Source="ip";Initial Catalog="";User Id="";Password="";Integrated Security=False");
        con.Open();
        using (var command = new SqlCommand(sql, con))
        {
            using (var reader = command.ExecuteReader())
            {
                var list = new List<Hatlar>();
                while (reader.Read())
                    list.Add(new Hatlar { HatId = reader.GetInt32(0), HatAdi = reader.GetString(1), LevhaAciklama = reader.GetString(2) });
                allRecords = list.ToArray();
            }
        }

Context.Response.Write( Newtonsoft.Json.JsonConvert.SerializeObject(allRecords));
      //this is what I'm using


Context.Response.Write( JavaScriptSerializer().Serialize(allRecords)); //your code


    }

Your webmethod contains string type so it will try to return string as response. so, we need to send response as an object.

I also had a same problem

Found solution from here https://stackoverflow.com/a/5392932/2630817

Community
  • 1
  • 1
Just code
  • 13,553
  • 10
  • 51
  • 93