Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:19:15+00:00 2026-05-27T01:19:15+00:00

I have a web service that is returning JSON. When testing the program, either

  • 0

I have a web service that is returning JSON. When testing the program, either through a browser or a test program, the string always has extra backslashes added. For example, if I am expecting my output to look like:

{"ack":{"qry":[{"retn":"abcd","desc":"defg"}] 

it is actually showing up as

{\"ack\":{\"qry\":[{\"retn\":\"abcd\",\"desc\":\defg\"}.

I am using JSON.NET and JsonTextWriter to create the string.

private string jsonParse(string respStr)
    {
        string jSonStr;
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        JsonWriter writer = new JsonTextWriter(sw);
        writer.Formatting = Formatting.None;

        List<string> commands = (from word in respStr.Split(':') select word).ToList();
        for (int j = 0; j < commands.Count; j += 3)
        {
            List<string> param = (from par in commands[j + 2].Split(new Char[] { ',', '=' }) select par).ToList();

            writer.WriteStartObject();
            writer.WritePropertyName(commands[j]);
            writer.WriteStartObject();
            writer.WritePropertyName(commands[j + 1]);
            writer.WriteStartArray();
            writer.WriteStartObject();
            for (int i = 0; i < param.Count - 1; i += 2)
            {
                writer.WritePropertyName(param[i]);
                writer.WriteValue(param[i + 1]);
            }
            writer.WriteEndObject();

            writer.WriteEndArray();
            writer.WriteEndObject();
            writer.WriteEndObject();
        }
        jSonStr = sb.ToString();

        return jSonStr;
    }

the operation contract for my web service looks like this:

[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json, UriTemplate="/{system}/{command}")]
string getMethod(string system, string command);

and finally the program I’m using to test uses HttpWebRequest like so:

HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";

Console.WriteLine("Sending GET Request");
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader sr = new StreamReader(GETResponseStream);

Console.WriteLine(sr.ReadToEnd());

any clue as to why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:19:15+00:00Added an answer on May 27, 2026 at 1:19 am

    Not sure why it is happenening (probably double encoding a string into a JSON string) but can I suggest that you would be better off not using JSON .Net for this and instead using DataContracts – it is much simpler and means your service does not need to know about the infrastructure it is running on. If you define your method like so:

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/{system}/{command}")]
    Response getMethod(string system, string command);
    

    And then do something like this for the Response object:

    [DataContract]
    public class Response
    {
        [DataMember(Name = "ack")]
        public Acknoweldge Acknowledge { get; set; }
    }
    
    [DataContract]
    public class Acknoweldge
    {
        [DataMember(Name = "qry")]
        public IEnumerable<Query> Query { get; set; }
    }
    
    [DataContract]
    public class Query
    {
        [DataMember(Name = "desc")]
        public string Description { get; set; }
    
        [DataMember(Name = "retn")]
        public string Return { get; set; }
    }
    

    Then implement your method like so (I just return a dummy object):

    public Response getMethod(string system, string command)
    {
        return new Response
            {
                Acknowledge = new Acknoweldge
                    {
                        Query = new List<Query>
                            {
                                new Query { Return = "abcd", Description = "defg" }
                            }
                    }
            };
    }
    

    Running your client code sample you will get the result you are looking for, specifically:

    {"ack":{"qry":[{"desc":"defg","retn":"abcd"}]}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the web service, which has method State() that returning the List. where
i have an ASP.NET web service that returning a custom entity object (Staff): [WebMethod]
I have a web service that I created in C# and a test harness
I have a web service that queries data from this json file, but I
I have a web service that uses Python's SimpleJSON to serialize JSON, and a
I have a web service that needs different settings for different environments (debug, test,
I have a .NET web service that is returning the following: <ArrayOfAddressLocation xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema
I have a restful WCF service that is returning JSON. I was wondering how
i have a web-service that will be returning a collection of System.Drawing.Image objects. My
I have a WCF web service that returns a string to a Java based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.