• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 53
  • Last Modified:

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

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
            },
0
PSCTECH
Asked:
PSCTECH
  • 4
  • 3
1 Solution
 
Fernando SotoRetiredCommented:
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.
0
 
PSCTECHAuthor 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
0
 
Fernando SotoRetiredCommented:
I assume this is a Web app?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
PSCTECHAuthor Commented:
yes
0
 
Fernando SotoRetiredCommented:
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

0
 
PSCTECHAuthor Commented:
Thank you very much for taking the time and putting in the effort on this.  I appreciate it very much.
0
 
Fernando SotoRetiredCommented:
Not a problem PSCTECH; glad I was able to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now