locked
Json code snippet RRS feed

  • Question

  • User136806914 posted

    Hi,

    I am new to JSon & trying to figure out stuff, any help is highly appreciate. I am loading Json object from Json external file that has a very complex structure like  arrarys, Json object & nested JSON objects. 

    I create a dynamic .net object & assigned deserialized object to it, something like

     dynamic test = JSSerializer.Deserialize<object>(json);

    I need to convert the JSOn object to a text file. How do i approach after JSON string is deserialized??

    Monday, October 10, 2016 6:26 AM

All replies

  • User136806914 posted

    Any suggestions??

    Monday, October 10, 2016 5:09 PM
  • User-474980206 posted
    Why don't you just write out the json string?
    Tuesday, October 11, 2016 12:54 AM
  • User-654786183 posted

    you can simply write the JSON string to a file

    System.IO.File.WriteAllText (@"D:\myjson.txt", json);

    Tuesday, October 11, 2016 1:01 AM
  • User-271186128 posted

    Hi PraveenAmar,

    dynamic test = JSSerializer.Deserialize<object>(json);

    I need to convert the JSOn object to a text file. How do i approach after JSON string is deserialized??

    After deserialized the JSON string, you could use StreamWriter to create text file and write the text content.

    Code like this:

           using (System.IO.StreamWriter file = 
                new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
            {
    //1.Array: Loop through the array and use the WriteLine method to write the array content.
    //2.Object: Get the properties of the Object.
    //3.Nested JSON Object: Similar Object, get the properties. foreach (string line in lines) { // If the line doesn't contain the word 'Second', write the line to the file. if (!line.Contains("Second")) { file.WriteLine(line); } } }

    More details, see:

    https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

    https://msdn.microsoft.com/en-us/library/system.io.file.aspx

    Best regards,
    Dillion

    Monday, October 24, 2016 9:34 AM