Home > Back-end >  How to convert a string to Json string in expected format using C#
How to convert a string to Json string in expected format using C#

Time:10-28

My input string is as follows:

inputstring= "[\n{\n\"Synonyms\": \"\\\"hm\\\",\\\"history matching\\\",\\\"HM\\\",\\\"History Matching\\\"\"\n    },\n{\n        \"Synonyms\": \"\\\"risk taking\\\",\\\"Risk Taking\\\",\\\"RISK TAKING\\\",\\\"risk-taking\\\",\\\"Risk-Taking\\\",\\\"RISK-TAKING\\\"\"\n    }\n]"

I need to create a function to generate the JSON string as follows.

Expected Output JSON is as follows:

{

  "wordAlterations": [
    {
      "alterations": [
        "hm",
        "history matching",
        "HM",
        "History Matching"
        ]
    },
    {
      "alterations": [
        "risk taking",       
        "Risk Taking",
        "RISK TAKING",
        "risk-taking",
        "Risk-Taking",
        "RISK-TAKING"

      ]
    }
  ]
}

Here is what I tried, but not working as expected.

[Fact]
public void InputStrToWordAlterationsTest2()
{
    string alterations = "[\n    {\n        \"Synonyms\": \"\\\"hm\\\",\\\"history matching\\\",\\\"HM\\\",\\\"History Matching\\\"\"\n    },\n    {\n        \"Synonyms\": \"\\\"genopt\\\",\\\"genOpt\\\",\\\"generic optimization\\\",\\\"Generic Optimization\\\"\"\n    }\n]";
    JArray jsonArray = JArray.Parse(alterations);
    var WordAlterations = new List<WordAlterationsDTO>(jsonArray.Count);
    var alterationsList2 = new List<AlterationsDTO>();
    var dto = new WordAlterationsDTO(alterationsList2);

    for (int i = 0; i <= jsonArray.Count - 1; i  )
    {
        WordAlterations.Add(dto);
    }

    string actual = JsonConvert.SerializeObject(dto);  //"{\"wordAlterations\":[]}"

    string expected = "{ 'wordAlterations': [ { 'alterations': [ 'hm','history matching', 'HM', 'History Matching' ] }, { 'alterations': [ 'risk taking', 'Risk Taking', 'RISK TAKING','risk - taking', 'Risk - Taking', 'RISK - TAKING' ] } ]}";
    Assert.Same(expected, actual);
}

Here are the model classes I use:

    public class AlterationsDTO
    {
        public AlterationsDTO();
        public AlterationsDTO(IList<string> alterations);

        [JsonProperty(PropertyName = "alterations")]
        public IList<string> Alterations { get; set; }

        public virtual void Validate();
    }

    public class WordAlterationsDTO
    {
        public WordAlterationsDTO();
        public WordAlterationsDTO(IList<AlterationsDTO> wordAlterations);

        [JsonProperty(PropertyName = "wordAlterations")]
        public IList<AlterationsDTO> WordAlterations { get; set; }

        public virtual void Validate();
    }

With my above code, I am transforming my input JSON string to formatted in the expected form.

CodePudding user response:

try this

    var jsonOrig="[\n{\n\"Synonyms\": \"\\\"hm\\\",\\\"history matching\\\",\\\"HM\\\",\\\"History Matching\\\"\"\n    },\n{\n        \"Synonyms\": \"\\\"risk taking\\\",\\\"Risk Taking\\\",\\\"RISK TAKING\\\",\\\"risk-taking\\\",\\\"Risk-Taking\\\",\\\"RISK-TAKING\\\"\"\n    }\n]";

    var json =jsonOrig.Replace("\\\"","");
        
    var jsonDeserialized=JsonConvert.DeserializeObject<Root[]>(json);
    
    var wordAlterations = new WordAlterations { wordAlterations= new List<WordAlteration>() };

    foreach (var element in jsonDeserialized)
        wordAlterations.wordAlterations.Add(new WordAlteration { alterations = element.Synonyms.Split(",").ToList()});

test

json=JsonConvert.SerializeObject(wordAlterations,Newtonsoft.Json.Formatting.Indented);

output

{
  "wordAlterations": [
    {
      "alterations": [
        "hm",
        "history matching",
        "HM",
        "History Matching"
      ]
    },
    {
      "alterations": [
        "risk taking",
        "Risk Taking",
        "RISK TAKING",
        "risk-taking",
        "Risk-Taking",
        "RISK-TAKING"
      ]
    }
  ]
}

classes

public class WordAlterations
{
    public List<WordAlteration> wordAlterations { get; set; }
}

public class WordAlteration
{
    public List<string> alterations { get; set; }
}

public class Root
{
    public string Synonyms { get; set; }
}

CodePudding user response:

First of all your input string is not a valid JSON format.

You can use JsonConvert.DeserializeObject.

  • Related