I've created a WCF Class Library which contains a function called SampleJSON(string name).
The Opertation Contract is like this :
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "sample/{name}")]
string SampleJSON(string name);
I intend to return valid JSON output from the function SampleJSON() and the return part is like this :
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
sw.Write("{");
sw.Write("\"" + "firstname" + "\"" + ":");
sw.Write("\"" + "samplefirstname" + "\"" + ",");
sw.Write("\"" + "lastname" + "\"" + ":");
sw.Write("\"" + "samplelastname" + "\"" + "}");
sw.Flush();
sw.Close();
return sb.ToString();
and the output that my service creates is something like this :
"{\"firtsname\":\"samplefirstname\",\"lastname\":\"samplelastname\"}"
which is obviusly invalid.
I've aslo tried to use Newton.JSON . but I still have the problem.
I think I need to return a JSONObject instead of string, but I don't know how to do this and the Docs of the Newton.JSON has not help me .
How can I produce a JSONObject that finally creates a valid JSON output, preferably using Newton.JSON.
[EDIT] this is the new code I'm using and the result on the browser :
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonTextWriter writer = new JsonTextWriter(sw);
writer.WriteStartObject();
writer.WritePropertyName("insertresult");
writer.WriteValue(resultArray[0]);
writer.WritePropertyName("insertmessage");
writer.WriteValue(resultArray[1]);
writer.WritePropertyName("sendsmsresult");
writer.WriteValue(resultArray[2]);
writer.WritePropertyName("sendsmsmessage");
writer.WriteValue(resultArray[3]);
writer.WriteEndObject();
writer.Flush();
sw.Flush();
return sb.ToString();
The result on the browser :
"{\"insertresult\":\"false\",\"insertmessage\":\"already existed\",\"sendsmsresult\":\"false\",\"sendsmsmessage\":\"noId\"}"
as you see this json is invalid.
This json is going to be used by an android application. My question is that : Does the android app see what I see here , or it sees the valid format ?