how do I add indents to the items in a list in .json (c#)

PSCTECH
PSCTECH used Ask the Experts™
on
I would like something like:

My list
       Item 1
       Item 2
       Item 3  

Here's my current non-indenting code
     {  
      "type": "paragraph",
                  "content": "The following have limitations on the amount that can be claimed:",
                  "listofitems": [
                        { "itemtext": "my text her" },
                        { "itemtext": "my second line of text" },
                        { "itemtext": "my final line of text" }
                  ],
                  "listunstyled": false,
                  "isunordered": true
            },
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Please explain /with more detail what you mean by, "add indents to the items in a list in .json", what type of project, what control are you trying to interact with and what version of Visual Studio are you using.

Author

Commented:
VS 2015

I am currently using a paragraph control with list of items. The indents I am talking about are shown below for Item1,2 and 3.  
I am open to using any control in the viewmodel .json that will result in text in the UI with spacing before it.  (see below example):

My list
       Item 1
       Item 2
       Item 3
Fernando SotoRetired
Distinguished Expert 2017

Commented:
I assume this is a Web app?

Author

Commented:
yes
Retired
Distinguished Expert 2017
Commented:
Hi PSCTECH;

Sorry it took a bit but have not done any web development in a while and getting old.
Added these two classes to the Models folder so to deserialize the json object into a .Net object
namespace WebApplication1.Models
{
    public class Rootobject
    {
        public string type { get; set; }
        public string content { get; set; }
        public Listofitem[] listofitems { get; set; }
        public bool listunstyled { get; set; }
        public bool isunordered { get; set; }
    }

    public class Listofitem
    {
        public string itemtext { get; set; }
    }

}

Open in new window

In the controller I materialized the json data into a .Net object and passed it to the view. Make sure that Newtonsoft.Json is installed and if not it can be installed through NuGet.
using WebApplication1.Models;
using Newtonsoft.Json;

public ActionResult Index()
{
    // Get the json string from where ever it is comming from
    string jsonMessage = "{      \"type\": \"pararaph\",    \"content\": \"The followin have limitations on the amount that can be claimed:\",    \"listofitems\": [        { \"itemtext\": \"Item 1 of the list of Items\" },        { \"itemtext\": \"Item 2 of the list of Items\" },        { \"itemtext\": \"Item 3 of the list of Items\" },        { \"itemtext\": \"Item 4 of the list of Items\" },        { \"itemtext\": \"Item 5 of the list of Items\" },        { \"itemtext\": \"Item 6 of the list of Items\" },        { \"itemtext\": \"my final Item\" }        ],    \"listunstyled\": false,    \"isunordered\": true}";
    // Convert it to a .Net object
    Rootobject jsonDataModel = JsonConvert.DeserializeObject<Rootobject>(jsonMessage);

    return View(jsonDataModel);
}

Open in new window

In the view I displayed the data as follows
@model WebApplication1.Models.Rootobject

@{
    ViewBag.Title = "Home Page";
}

...

<div>
    <p>
        @if (Model != null)
        {
            if (Model.listofitems != null)
            {
                <label>My List</label><br/>
                foreach (var item in Model.listofitems)
                {
                    <label>&nbsp&nbsp&nbsp @item.itemtext.ToString()</label><br/>
                }

            }
        }
    </p>
</div>

Open in new window

Author

Commented:
Thank you very much for taking the time and putting in the effort on this.  I appreciate it very much.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem PSCTECH; glad I was able to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial