views:

548

answers:

6

Hi

My code works (yeah!) which sends json to a server.. would appreciate any thoughts on refactoring

1) My C# code sends this json to the server

{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"[email protected]\",\"deviceUUID\":\"abcdefghijklmnopqrstuvwxyz\"}

Which I have to get rid of the slashes on the server side....not good.

2) I'm using application/x-www-form-urlencoded and probably want to be using application/json

Person p = new Person();
            p.firstName = "Bill";
            p.lastName = "Gates";
            p.email = "[email protected]";
            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'};
+1  A: 

Are you sure you have those slashes in there? That's the debugger view which C# encodes the string for display, but the real values coming out of JavaScriptSerializer don't have any slashes in the identifier. The only thing that gets escaped is the JSON value content...

Rick Strahl
A: 

Hi

Good thoughts... I've checked in VS and when set a breakpoint on textBox3.Text = s; and then hover over the previous line s. It shows this:

s = "{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"[email protected]\",\"deviceUUID\":\"abcdefghijklmnopqrstuvwxyz\"}"

So, I've checked on the PHP side too:

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

string(124) "{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"[email protected]\",\"deviceUUID\":\"abcdefghijklmnopqrstuvwxyz\"}"

Dave
A: 

Thanks Kenny... was trying to keep it simple by using the inbuilt library in 3.5SP1.. but maybe..!

Cheers

Dave
A: 

JSON.NET is great and well worth working out - it does a few things better than JavaScriptSerializer, but I know for a fact that JavaScriptSerializer (and any other JSON serializer) will not output identifiers with slashes. It'd be invalid JSON.

Rick Strahl
A: 

Got it

There were 2 problems:

The PHP server was escaping incoming data, so I had to use stripslashes.

http://nz.php.net/magic_quotes

Also hovering over the property in VS showed slashed but a

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.

Dave