1

I Create a simple WCF service and he works fine. The configuration are below The interface have this configuration

   [OperationContract]
    [WebInvoke(Method = "GET",
                  ResponseFormat = WebMessageFormat.Json,
                  BodyStyle = WebMessageBodyStyle.Bare,
                  UriTemplate = "checkSymbolExistJson/{pSymbol}")]
     string checkSymbolExistJson(string pSymbol);

The implementation is this

         public string checkSymbolExistJson(string pSymbol)
      {
            Person p    = new Person();
            p.name      = pSymbol;
            p.age       = 15;

        string json = JsonConvert.SerializeObject(p);
        return json;
      }

if I enter URL in browser "http://localhost/MetaTraderWcf/rzManageQuotes.svc/checkSymbolExistJson/testename" in brower I Get this result in Browser

"{\"name\":\"testename\",\"age\":15}"

After I make a win 32 application to get http result of this WCF service. I use this code to read a HTML page

        public string readUrl(string pUrl)
    {
        WebClient client = new WebClient { Encoding = System.Text.Encoding.UTF8 };
        return client.DownloadString(pUrl);
    }

I use this code to read a JSON dinamic TAG

       private void button2_Click(object sender, EventArgs e)
    {

         string tmpToken = readUrl(url.Text);
        // string tmpToken = "{\"name\":\"testename\",\"age\":15}";
        JToken token = JObject.Parse(tmpToken);

              string page = (string)token.SelectToken("name");
              jSONResult.Text = page;

    }

if I Runing code above with fixed code below

string tmpToken = "{\"name\":\"testename\",\"age\":15}";

The result is correct and I get result as "testename".

But when I Debug the read a Html page I receive tha value of tmpToken with this string

"\"{\\"name\\":\\"testename\\",\\"age\\":15}\""

And I get a error when I read dinamic value of name

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll

Additional information: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 37.

If I change interface to return a XML page like this code

       [OperationContract]
     [WebInvoke(Method = "GET",
                  ResponseFormat = WebMessageFormat.Xml,
                  BodyStyle = WebMessageBodyStyle.Bare,
                  UriTemplate = "checkSymbolExistJson/{pSymbol}")]
     string checkSymbolExistJson(string pSymbol);

I get the follow result in browser

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{"name":"testename","age":15}</string>

And I get Read JSON value of name correct after remove tag from XML result.

The Question is There is some way of read a string in pure JSON format in c# like a read in a Browser format like this

{"name":"testename","age":15}

and not like this format

"\"{\\"name\\":\\"testename\\",\\"age\\":15}\""

1
  • Your "result in the browser" should not contain quotes at the outside, or backslashes. Also note that the debugger helpfully shows the string "escaped" as you could write it in sourcecode. Commented Mar 7, 2017 at 10:01

1 Answer 1

3

there is a simple solution for that. just return stream except string.

     public stream checkSymbolExistJson(string pSymbol)
  {
        Person p    = new Person();
        p.name      = pSymbol;
        p.age       = 15;

    string json = JsonConvert.SerializeObject(p);
    return new MemoryStream(Encoding.UTF8.GetBytes(json));
  }

or i suggest use web API instead WCF.

Sign up to request clarification or add additional context in comments.

2 Comments

Your code Works !!! But have a small note, if I request the URL in google chrome, chrome download a file, If I execute the URL in I.E or EDGE the Result is Text, and Finaly if I read the code with Win 32 Application The result is Ok . than the only one problem is chrome process. Thank you.
I'm glad i could help. chrome or other web browsers design to download the stream. they cant determine the stream is a simple string or a file. so they just download it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.