简体   繁体   中英

converting Json data to c# understandable format

I am trying to convert my json data to c sharp understandable format so that I can use JsonConvert.SerializeObject to convert that data to JSON format and send it over HTTP protocol. My json data is as follows:

 {
    "m2m:ae":
    {
     "api": "ADN_AE_ATCARD06",
     "rr": "true",
     "lbl": ["AT06"],
     "rn": " adn-ae_AT06" 
   }
 }

I tried to write it in c sharp understandable format but was able to do this:

 var obj = new
            {
               m2m = new
                {
                    api = "ADN_AE45",
                    rr = "true",
                    lbl = new[] { "ad" },
                    rn = "adfrt"

                }
            };
            var result = JsonConvert.SerializeObject(obj, Formatting.Indented);

my issue is how to include m2m:ae into my c-sharp code . I am new to json, I am able to convert only if parent object has no value but if it has value I am not able to. please help.

I was incorrect in my comment. While "m2m:ae" is not a valid name for a C# property is valid JSON. This can be accomplished by tagging the class property with JsonProperty.

Define your class like this

public class TestJson
{
    [JsonProperty("m2m:ae")]
    public Class2 Class2Instance { get; set; }    

}

public class Class2
{
    public string Api { get; set; }
    public string Rr { get; set; }
    public string[] Lbl { get; set; }

    public string Rn { get; set; }
}

and then populate your class like this

            _sut = new TestJson
            {
                Class2Instance = new Class2 {Api = "ADN_AE45", Rr = "true", Lbl = new[] {"ad"}, Rn = "adfrt"}
            };

and serialize

            _result = JsonConvert.SerializeObject(_sut, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

The resulting serialized object looks like

{
  "m2m:ae": {
    "api": "ADN_AE45",
    "rr": "true",
    "lbl": ["ad"],
    "rn": "adfrt"
  }
}

and deserialization is the reverse

            var test = JsonConvert.DeserializeObject<TestJson>(_result, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

Did you write the Json in a text file? If yeah so fileTxt is what is written on the file(as json)

String fileTxt = File.ReadAllText(Give a path for a file); //read from file
Jobject Json = Jobject.Parse(fileTxt);   //convert the string to Json
Console.WriteLine($"rr: {Json["rr"]} ");  // get the rr from json and print it

Other than class method mentioned by Fran one can use the Dictionary method to create key value pair and newtonsoft.json library to convert to json.

var sub= Dictionary<string,string>
{{"abc","xyz"}
};
var main = Dictionary<string,object>
{{"wer:asd",sub}
};
string str= JsonConvert.SerializeObject(main);

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