How to Make Some Text from a Json String Bold

Bold words in a string of json file in Android

JSON only stores plain text, It does not format the text, if you want to make the data in the JSON object appear bold or colorful in the UI you need to handle this from the front end side.

Can a json string contain HTML tags with it?

Technically, yes, you can do that... practically, I'd be a bit concerned if there were HTML markup in my data. What else might be in there? Smells like an XSS vulnerability.

Bold Specific Elements from JSON Array in Rich Textbox

You need to use AppendText. When you use Text+= "something" you replace the format.

You can use this example:

var json = "{\"Title\":\"This is a Title\", \"Content_One\": \"This is alittle informative paragraph based on the subject selected\", \"Title_Two\": \"This is another paragraph\"}";
var start = 0;
Dictionary<string, string> values = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(json);

values.Cast<KeyValuePair<string, string>>()
.ToList()
.ForEach(item =>
{
this.richTextBox1.AppendText(item.Key);
this.richTextBox1.AppendText( ":" );
start += item.Key.Length + 1;
this.richTextBox1.AppendText(item.Value);
this.richTextBox1.Select(start, item.Value.Length);
this.richTextBox1.SelectionFont = new Font(this.richTextBox1.Font, FontStyle.Bold);
this.richTextBox1.AppendText("\n");
start += item.Value.Length + 1;
});

Screenshot:

Sample Image



Related Topics



Leave a reply



Submit