简体   繁体   中英

deserialize json file with base64 encoded file attachments

I am trying to deserialize base64 file attachments from JSON file using JSON.NET. I can deserialize the data using the following code but I am not sure how to decode the base64 files. Please let me know.

Sample JSON file
-----------------
PostData: {
  "Main":{
     "date1": "14-JAN-2016",
     "summary": "summary test",
     "Details":[{
       "first": "test",
       "last": "last test",
        },
      ]}
     "attachments":[        {
       "title": "Report1.pdf",
       "file_name": "Report1.pdf",
       "file_content_type": "application/pdf",
       "file_format": "binary",
       "file_data": "JVBERi0xLjQNCjUgMCBvYmoNCjw8DQovVHlwZSAvWE9iamVjdA0KL1N1YnR5cGUg
        MSAwIFIvSW5mbyAxMjYgMCBSL0lEWzw0NTQ3QjRFREZBNjk2NDQ4QjhBNUU4MUQw
        QkU4MkVDMD48NDU0N0I0RURGQTY5NjQ0OEI4QTVFODFEMEJFODJFQzA+XSAvUHJl
        diAxMTI3OTI5L1hSZWZTdG0gMTEyNjMxNj4+DQpzdGFydHhyZWYNCjExNDAyNTEN
        CiUlRU9G",
        },
        {
       "title": "src.txt",
       "file_name": "src.txt",
       "file_content_type": "text/plain",
       "file_format": "text",
       "file_data": "bm5gLDJ9NC1DNFkpN20y4tLmM0",
        },
  ]}
}

Program.cs

var json = System.IO.File.ReadAllText("postData.json");
var myJsonObject = JsonConvert.DeserializeObject<PostData>(json);

    public class PostData
    {
        public Main main { get; set; }
    }

    public class Main
    {

        public string date1 { get; set; }
        public string summary { get; set; }
        public List<Details> details { get; set; }
    }

  public class Details
    {
        public string first { get; set; }
        public string last { get; set; }
    }

As explained in its Serialization Guide , Json.NET automatically (de)serializes Byte [] arrays as Base64 strings. Thus, after cleaning up your JSON you can upload it to http://json2csharp.com/ to auto-generate classes, then modify the file_data property to be of type byte [] with the result shown below:

public class Detail
{
    public string first { get; set; }
    public string last { get; set; }
}

public class Main
{
    public string date1 { get; set; }
    public string summary { get; set; }
    public List<Detail> Details { get; set; }
}

public class Attachment
{
    public string title { get; set; }
    public string file_name { get; set; }
    public string file_content_type { get; set; }
    public string file_format { get; set; }
    public byte [] file_data { get; set; }
}

public class PostData
{
    public Main Main { get; set; }
    public List<Attachment> attachments { get; set; }
}

Now you can deserialize as follows:

var myJsonObject = JsonConvert.DeserializeObject<RootObject>(json);

Then, to save the deserialized byte arrays to disk, you could do something like the following with File.WriteAllBytes()

foreach (var attachment in myJsonObject.PostData.attachments ?? Enumerable.Empty<Attachment>())
{
    File.WriteAllBytes(attachment.file_name, attachment.file_data);
}

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