Question

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 ?

Was it helpful?

Solution

If you want to return complex types in WCF and have them be serialized correctly to JSON, you need to create a data contract. A data contract is simply a class to contain the data to be serialized, where the class is marked with a [DataContract] attribute and each member of that class you want to be serialized is marked with a [DataMember] attribute. For example:

[DataContract]
public class Person
{
    [DataMember(Name="firstname")]
    public string FirstName { get; set; }

    [DataMember(Name="lastname")]
    public string LastName { get; set; }
}

With the data contract in place, change your service method to build and return an instance of that class instead of a string. For example:

Person SampleJSON(string name)
{
    Person p = new Person
    {
        FirstName = "samplefirstname",
        LastName = "samplelastname"
    }
    return p;
}

WCF will handle the serialization of the data to JSON automatically. If you try to serialize it manually first, then you will get double-serialized string which contains backslashes, as you have seen.

If you have some reason that you need to handle the serialization manually, then you will either have to replace or work around WCF's serializer. See this answer for a starting point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top