0

I am trying to construct a string which I want to use it to update a JSON.

The code is below

  public string ConstructCylicLoop(string fieldName, int LoopCount, string BadDataLabel,string ImmediateParent)
    {
        string start = "";
        string fullbody = "";
        string end = "";
        string body = "";
        for (int i = 0; i < LoopCount; i++)
        {
            LoopTestData = (new ExcelUtilities().getAPITestData(ApplicationConfiguration.LoopSheetName));
            body = "";
            foreach (Dictionary<string, string> loopData in LoopTestData)
            {
                string ParentNode = "";
                string Key = "";
                string Data = "";                    
                loopData.TryGetValue("ParentNode", out ParentNode);
                loopData.TryGetValue("Key", out Key);
                loopData.TryGetValue("Data", out Data);
                if(ImmediateParent.Equals(ParentNode)) //&& Key.Equals(fieldName)
                {
                    body = body + '"' + Key + '"' + ":" + '"' + Data + '"'+',';
                }
            }
            body = body.Remove(body.Length - 1);
            body = "{" + body + "},";
            fullbody = fullbody + body;
        }            
        fullbody = fullbody.Remove(fullbody.Length - 1);            
        return start + fullbody + end;
    }

The issue with this code is it always returns a text like this

"{\"my_address_type\":\"primarypropertyaddress\",\"my_address-street\":\"52 Street\",\"my_address-suburb\":\"vinvent\",\"my_address-postcode\":\"2121\"}"

When I update this string to an JSON node, the server is not able to parse it and the issue is with the back slash. Is there a way to remove the back slash. so I get something like this..

"{"my_address_type":"primarypropertyaddress","my_address-street":"52 Street","my_address-suburb":"vinvent","my_address-postcode":"2121"}"

I tried all possibilities but not able to clear/remove the backslash. Any code snippet on removing the backslashes. Thanks in advance.

Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62
  • 3
    Any reason for manually creating JSON strings? I mean, we are not in 1960... Also, where exactly are you seeing that output? – Camilo Terevinto May 27 '18 at 23:05
  • 3
    Those are just escape characters, they don't actually appear in the output. – Ron Beyer May 27 '18 at 23:05
  • Can't you just serialize your JSON data? ["How to Convert JSON data to object](https://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) – Eryk Korzeniowski May 27 '18 at 23:06
  • Try [newtonsoft]( https://www.newtonsoft.com/json ) – Ahmed Soliman May 27 '18 at 23:07
  • @CamiloTerevinto The reason is I am doing a data driven testing with updating the node with all possible combinations. Just update the json node and send it to api end point... – Timothy Rajan May 27 '18 at 23:15
  • @RonBeyer - Its appearing in the output and not able to get rid of them... – Timothy Rajan May 27 '18 at 23:16
  • 1
    Write it out to a text file, I bet it isn't there, you just see it in the debugger. – Ron Beyer May 27 '18 at 23:17
  • @RonBeyer - When I save it to a txt file, I am able to see the slashes. The bolded text is copied from the txt file. – Timothy Rajan May 27 '18 at 23:28
  • This is one of the few times when you have to post a screenshot. – Dour High Arch May 28 '18 at 00:11
  • Possible duplicate of [How to create JSON string in C#](https://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp) – mjwills May 28 '18 at 00:55
  • @TimothyRajan: I don't see a connection between "I want to be able to update the JSON node" and "I need to build JSON manually rather than using a well-tested, well-trusted library". I would *definitely* recommend using Json.NET or similar. But beyond that, a [mcve] would make it a lot easier to help you - we don't know how that dictionary is being populated. – Jon Skeet May 28 '18 at 05:49

0 Answers0