【问题标题】:JSON - Slashes and application type..refactoringJSON - 斜杠和应用程序类型..重构
【发布时间】:2010-09-23 09:14:25
【问题描述】:

我的代码有效(是的!)它将 json 发送到服务器。如果有任何重构的想法,我将不胜感激

1) 我的 C# 代码将此 json 发送到服务器

{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"asdf@hotmail.com\",\"deviceUUID\": \"abcdefghijklmnopqrstuvwxyz\"}

我必须去掉服务器端的斜线....不好。

2) 我正在使用 application/x-www-form-urlencoded 并且可能想要使用 application/json

Person p = new Person();
            p.firstName = "Bill";
            p.lastName = "Gates";
            p.email = "asdf@hotmail.com";
            p.deviceUUID = "abcdefghijklmnopqrstuvwxyz";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri + "newuser.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            //TODO request.ContentType = "application/json";

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string s = serializer.Serialize(p);
            textBox3.Text = s;

            string postData = "json=" + HttpUtility.UrlEncode(s);

            byte[] byteArray = Encoding.ASCII.GetBytes(postData);

            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close ();

            WebResponse response = request.GetResponse();
            //textBox4.Text = (((HttpWebResponse)response).StatusDescription);
            dataStream = response.GetResponseStream ();

            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd ();
            textBox4.Text += responseFromServer;

            reader.Close ();
            dataStream.Close ();
            response.Close ();

服务器上的 PHP 代码:

$inbound = $_POST['json'];

// this strips out the \
$stripped = stripslashes($inbound);

$json_object = json_decode($stripped);
echo $json_object->{'firstName'};
echo $json_object->{'lastName'};
echo $json_object->{'email'};
echo $json_object->{'deviceUUID'};

【问题讨论】:

    标签: c# php json refactoring


    【解决方案1】:

    你确定你有那些斜线吗?这是 C# 对字符串进行编码以供显示的调试器视图,但来自 JavaScriptSerializer 的实际值在标识符中没有任何斜杠。唯一被转义的是 JSON 值内容...

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      好主意...我已经在 VS 中检查过,并且在 textBox3.Text = s; 上设置断点时 然后将鼠标悬停在上一行 s 上。它显示了这一点:

      s = "{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"asdf@hotmail.com\",\"deviceUUID \":\"abcdefghijklmnopqrstuvwxyz\"}"

      所以,我也检查了 PHP 方面:

      $inbound = $_POST['json'];
      var_dump($inbound);
      

      字符串(124) "{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"asdf@hotmail.com\",\"deviceUUID\":\" abcdefghijklmnopqrstuvwxyz\"}"

      【讨论】:

        【解决方案4】:

        JSON.NET 非常棒,值得一试——它比 JavaScriptSerializer 做得更好,但我知道 JavaScriptSerializer(和任何其他 JSON 序列化器)不会输出带有斜杠的标识符。这将是无效的 JSON。

        【讨论】:

          【解决方案5】:

          知道了

          有两个问题:

          PHP 服务器正在转义传入的数据,所以我不得不使用带斜杠。

          http://nz.php.net/magic_quotes

          同样悬停在 VS 中的属性上显示斜线,但是一个

          Debug.Write(s);
          

          显示:

          [{
            "categoryid": "1",
            "name": "funny",
            "serverimageid": "1",
            "dateuploaded": "2008-11-17 16:16:41",
            "enabled": "\u0001"
          }, {
            "categoryid": "2",
            "name": "happy",
            "serverimageid": "2",
            "dateuploaded": "2008-11-17 16:17:00",
            "enabled": "\u0001"
          }, {
            "categoryid": "3",
            "name": "sad",
            "serverimageid": "3",
            "dateuploaded": "2008-11-16 16:17:13",
            "enabled": "\u0001"
          }]
          

          谢谢大家。

          【讨论】:

            猜你喜欢
            • 2013-07-10
            • 1970-01-01
            • 1970-01-01
            • 2018-09-27
            • 2016-11-23
            • 1970-01-01
            • 2018-07-26
            • 2017-06-13
            • 1970-01-01
            相关资源
            最近更新 更多