In my scenario I have list of object in C# code and need to be converted into JavaScript object. But there are certain condition, where the value of the object might be dynamic based on certain key.
I have a following method which will return string as JavaScript Object.
public string ItemToJson()
{
List < Item > itemObj = GetItemList();
if (itemObj.Count > 0)
{
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = {");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
{
sbObj.Append(itemObj[i].Key);
sbObj.Append(": { placeholder : \" ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" \" , value : \" ");
if (itemObj[i].Key == "Photo")
{
sbObj.Append(GetImage());
}
else
{
sbObj.Append(itemObj[i].Value);
}
sbObj.Append(" \" } ");
if (i < len - 1)
sbObj.Append(",");
}
sbObj.Append("} </script>");
return sbObj.ToString();
}
else
{
return string.Empty;
}
}
Here is the complete code of working console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String_Builder_Demo
{
class Program
{
public List<Item> GetItemList()
{
List<Item> items = new List<Item>
{
new Item(){ Key = "FirstName", Placeholder = "##FirstName##", Value="John" },
new Item(){ Key = "LastName", Placeholder = "##LastName##", Value="Doe"},
new Item(){ Key = "Email", Placeholder = "##Email##", Value="[email protected] " },
new Item(){ Key = "Address", Placeholder = "##Address##", Value="Kathmandu" },
new Item(){ Key = "Photo", Placeholder = "##Photo##", Value=""}
};
return items;
}
public string GetImage()
{
return "http://via.placeholder.com/350x150";
}
static void Main(string[] args)
{
Program obj = new Program();
Console.WriteLine(obj.ItemToJson());
Console.ReadLine();
}
public string ItemToJson()
{
List<Item> itemObj = GetItemList();
if (itemObj.Count > 0)
{
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = {");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
{
sbObj.Append(itemObj[i].Key);
sbObj.Append(": { placeholder : \" ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" \" , value : \" ");
if (itemObj[i].Key == "Photo")
{
sbObj.Append(GetImage());
}
else
{
sbObj.Append(itemObj[i].Value);
}
sbObj.Append(" \" } ");
if (i < len - 1)
sbObj.Append(",");
}
sbObj.Append("} </script>");
return sbObj.ToString();
}
else
{
return string.Empty;
}
}
}
public class Item
{
public string Placeholder { get; set; }
public string Value { get; set; }
public string Key { get; set; }
}
}
The above mention code will return following string.
<script>
let Items = {
FirstName:
{
placeholder: " ##FirstName## ",
value: " John "
},
LastName:
{
placeholder: " ##LastName## ",
value: " Doe "
},
Email:
{
placeholder: " ##Email## ",
value: " [email protected] "
},
Address:
{
placeholder: " ##Address## ",
value: " Kathmandu "
},
Photo:
{
placeholder: " ##Photo## ",
value: " http://via.placeholder.com/350x150 "
}
}
</script>
How can I optimize the above code and eliminate the if else
condition from ItemToJson()
method?
##Photo##
so I thing its impossible to use json.net \$\endgroup\$if else
in this case ? \$\endgroup\$if/else
statement? What's wrong with it? \$\endgroup\$