json的反应是否正确?

时间:2014-09-25 04:24:08

标签: c# asp.net json wcf

我正在使用此代码

    public static string myjson(bool a)
    {
        if (a == true)
        {
            RootObject o = new RootObject();

            o.success = "true";
            o.firstname = "anurag";
            o.usertype = "admin";
            o.userid = "123";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
             string d=serializer.Serialize(o);
             return d;

        }
        else
        {
            errormessage o = new errormessage();

              o.success = "false";
            o.message = "invalid username or password";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
           string p=serializer.Serialize(o);
            return p.ToString();


        }
    }

当我在iis上托管我的网络服务并且超出此服务时我得到回应

      {"checkuserResult":" {\"success\":\"true\",\"firstname\":\"anurag\",\"userid\":\"123\",\"usertype\":\"admin\"}"}  

我想要的回报只是json回复

       {"success":"true","firstname":"anurag","userid":"123","usertype":"admin"}  

请帮帮忙?

1 个答案:

答案 0 :(得分:0)

我建议您使用JSON.Net.Far比JavascriptSerializer更好,您可以非常好地控制行为。

首先安装Nuget:

  

Install-Package Newtonsoft.Json

然后致电

  

var json = JsonConvert.SerializeObject(o);