简体   繁体   中英

"/" in result when serializing a JSON object

The Problem

I try to convert a C# class to a JSON object .

I have tried it using the JsonConvert.SerializeObject function. But I always end up with \\ in the result.

Serialization

string data = JsonConvert.SerializeObject(rootObject);

C# Class

public class JiraApiObject
{
    public RootObject rootObject { get; set; }

    public class Project {
        public string key { get; set; }
    }

    public class Issuetype {
        public string name { get; set; }
    }

    public class Fields {
        public Project project { get; set; }
        public string summary { get; set; }
        public Issuetype issuetype { get; set; }
    }

    public class RootObject  {
        public Fields fields { get; set; }
    }
}

Actual Result

This is the result returned when I serialize the RootObject of the JiraApiObject. Full of reverse slashes.

    "{\
    "fields\":{
      \"project\":
      {
        \"key\":\"FOO\"
      },
      \"summary\":\"Test the REST API\",
      \"issuetype\": {
        \"name\":\"Task\"
      }
    }
}"

Expected Result

    {
    "fields": {
       "project":
       {
          "key": "FOO"
       },
       "summary": "Test the REST API",
       "issuetype": {
          "name": "Task"
       }
      }
    }

What is the best way to get rid of these \\ in the actual result?

The backspaces could be a by-product of the debuggers presentation of a JSON object as a string. If your debugger has a JSON visualizer (which VS does) that should allow you to validate your JSON object, otherwise try outputting it to a file or to the console to check the formatting. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM