Step 1: Add following c# code in you class file:
JSON:
{
"Vendor": {
"FirstName": "Bob",
"LastName": "Smith",
"Saultation": "Mr.",
"BusinessName": "Some Company Pty. Ltd."
},
"Property": {
"DisplayAddress": "1 My Street, My Suburb, 0000",
"Suburb": "My Suburb",
"StreetName": "My Street",
"StreetNumber": "1"
},
"Vpa": {
"TotalAmount": "12345.00",
"EndDate": "01-01-2020"
},
"Campaign": {
"DisplayAddress": "USA"
}
}
HTML:
<div>
Dear {{Vendor.FirstName}} {{Vendor.LastName}},
<br><br>
Thank you for joining us by listing your property {{Campaign.DisplayAddress}}.
<br><br>
As per our agreement your payment of ${{Vpa.TotalAmount}} is due at settlement.
<br><br>
Thank you. The team.
</div>
Step 3: Now time to test our c# code to merge above html with json. Paste the following code in you controller class:
public ActionResult Index()
{
MergeHtmlWithJson();
return View();
}
public string MergeHtmlWithJson()
{
using (StreamReader reader = new StreamReader(Server.MapPath("/Content/sample.json")))
{
string jsonValues = reader.ReadToEnd();
string htmlTemplate = new StreamReader(Server.MapPath("/Content/sample.html")).ReadToEnd();
return AppUtils.MergeHtmlWithJson(jsonValues, htmlTemplate);
}
}
public class AppUtils
{
public static string MergeHtmlWithJson(string json, string htmlTemplate)
{
JObject parsedJson = JObject.Parse(json);
Dictionary<string, string> jsonKeys = ConvertJsonToDictionary(parsedJson, new Dictionary<string, string>());
string keyToReplace = string.Empty;
foreach (var key in jsonKeys)
{
keyToReplace = "{{" + key.Key + "}}";
htmlTemplate = htmlTemplate.Replace(keyToReplace, key.Value);
}
return htmlTemplate;
}
private static Dictionary<string, string> ConvertJsonToDictionary(JObject parsedJson, Dictionary<string, string> jsonKeys, string path = "")
{
foreach (var rootItem in parsedJson)
{
if (rootItem.Value.HasValues)
{
foreach (var value in rootItem.Value)
{
string keyValue = string.Empty;
if (value.Count() == 1)
{
keyValue = value.First.ToString();
if (keyValue.Contains("{") || keyValue.Contains("}"))
{
string[] arrayPath = value.Path.Split('.');
if (arrayPath.Length > 1)
{
arrayPath = arrayPath.Take(arrayPath.Count() - 1).ToArray();
}
string innerPropPath = string.Join(".", arrayPath);
innerPropPath = !string.IsNullOrWhiteSpace(path) ? $"{path}.{innerPropPath}" : innerPropPath;
ConvertJsonToDictionary(JObject.Parse("{" + value.ToString() + "}"), jsonKeys, innerPropPath);
}
else
{
if (!string.IsNullOrWhiteSpace(path))
{
jsonKeys[$"{path}.{value.Path}"] = keyValue;
}
else
{
jsonKeys[value.Path] = keyValue;
}
}
}
else
{
foreach (var item in value)
{
keyValue = item.First.ToString();
if (!string.IsNullOrWhiteSpace(path))
{
jsonKeys[$"{path}.{item.Path}"] = keyValue;
}
else
{
jsonKeys[item.Path] = keyValue;
}
}
}
}
}
else
{
jsonKeys[rootItem.Value.Path] = rootItem.Value.ToString();
}
}
return jsonKeys;
}
}
Step 2: Create one html and one json file and then paste the following json content into json file and html content into html file:{
public static string MergeHtmlWithJson(string json, string htmlTemplate)
{
JObject parsedJson = JObject.Parse(json);
Dictionary<string, string> jsonKeys = ConvertJsonToDictionary(parsedJson, new Dictionary<string, string>());
string keyToReplace = string.Empty;
foreach (var key in jsonKeys)
{
keyToReplace = "{{" + key.Key + "}}";
htmlTemplate = htmlTemplate.Replace(keyToReplace, key.Value);
}
return htmlTemplate;
}
private static Dictionary<string, string> ConvertJsonToDictionary(JObject parsedJson, Dictionary<string, string> jsonKeys, string path = "")
{
foreach (var rootItem in parsedJson)
{
if (rootItem.Value.HasValues)
{
foreach (var value in rootItem.Value)
{
string keyValue = string.Empty;
if (value.Count() == 1)
{
keyValue = value.First.ToString();
if (keyValue.Contains("{") || keyValue.Contains("}"))
{
string[] arrayPath = value.Path.Split('.');
if (arrayPath.Length > 1)
{
arrayPath = arrayPath.Take(arrayPath.Count() - 1).ToArray();
}
string innerPropPath = string.Join(".", arrayPath);
innerPropPath = !string.IsNullOrWhiteSpace(path) ? $"{path}.{innerPropPath}" : innerPropPath;
ConvertJsonToDictionary(JObject.Parse("{" + value.ToString() + "}"), jsonKeys, innerPropPath);
}
else
{
if (!string.IsNullOrWhiteSpace(path))
{
jsonKeys[$"{path}.{value.Path}"] = keyValue;
}
else
{
jsonKeys[value.Path] = keyValue;
}
}
}
else
{
foreach (var item in value)
{
keyValue = item.First.ToString();
if (!string.IsNullOrWhiteSpace(path))
{
jsonKeys[$"{path}.{item.Path}"] = keyValue;
}
else
{
jsonKeys[item.Path] = keyValue;
}
}
}
}
}
else
{
jsonKeys[rootItem.Value.Path] = rootItem.Value.ToString();
}
}
return jsonKeys;
}
}
JSON:
{
"Vendor": {
"FirstName": "Bob",
"LastName": "Smith",
"Saultation": "Mr.",
"BusinessName": "Some Company Pty. Ltd."
},
"Property": {
"DisplayAddress": "1 My Street, My Suburb, 0000",
"Suburb": "My Suburb",
"StreetName": "My Street",
"StreetNumber": "1"
},
"Vpa": {
"TotalAmount": "12345.00",
"EndDate": "01-01-2020"
},
"Campaign": {
"DisplayAddress": "USA"
}
}
HTML:
<div>
Dear {{Vendor.FirstName}} {{Vendor.LastName}},
<br><br>
Thank you for joining us by listing your property {{Campaign.DisplayAddress}}.
<br><br>
As per our agreement your payment of ${{Vpa.TotalAmount}} is due at settlement.
<br><br>
Thank you. The team.
</div>
Step 3: Now time to test our c# code to merge above html with json. Paste the following code in you controller class:
public ActionResult Index()
{
MergeHtmlWithJson();
return View();
}
public string MergeHtmlWithJson()
{
using (StreamReader reader = new StreamReader(Server.MapPath("/Content/sample.json")))
{
string jsonValues = reader.ReadToEnd();
string htmlTemplate = new StreamReader(Server.MapPath("/Content/sample.html")).ReadToEnd();
return AppUtils.MergeHtmlWithJson(jsonValues, htmlTemplate);
}
}
No comments:
Post a Comment