JSON - Slashes and application type .. refactoring
My code works (yes!) Which sends json to the server .. any refactoring thoughts will be appreciated
1) My C # code is sending this json to the server
{\ "Name \": \ "Bill \", \ "LastName \": \ "Gate \", \ "Email \": \ " asdf@hotmail.com \", \ "deviceUUID \": \ " ABVGDEZHZIKLMNOPRSTUFKHCHSHEYUYA \ "}
Which I have to get rid of the forward slashes on the server side .... not good.
2) I am using application / x-www-form-urlencoded and probably want to use 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 code on server:
$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'};
source to share
I found it easy to use: http://james.newtonking.com/archive/2008/02/11/linq-to-json-beta.aspx and http://james.newtonking.com/projects/json-net .aspx
source to share
Good thoughts ... I checked VS and set a breakpoint on textBox3.Text = s; and then hover your cursor over the previous line s. He shows this:
s = "{\" firstName \ ": \" Bill \ ", \" lastName \ ": \" Gates \ ", \" email \ ": \" asdf@hotmail.com \ ", \" deviceUUID \ ": \ "ABVGDEZHZIKLMNOPRSTUFHCHSHEYUYA \"} "
So, I also checked on the PHP side:
$inbound = $_POST['json'];
var_dump($inbound);
line (124) "{\" Name \ ": \" Bill \ "\" LastName \ ": \" Gate \ ", \" email \ ": \" asdf@hotmail.com \ ", \" deviceUUID \ ": \" ABVGDEZHZIKLMNOPRSTUFHCHSHEYUYA \ "}"
source to share
Got it
There were 2 problems:
The PHP server was avoiding incoming data, so I had to use stripslashes.
http://nz.php.net/magic_quotes
Also, hovering over a property in VS showed a shortcut, but
Debug.Write(s);
showed:
[{"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"}]
Thanks everyone.
source to share