0

How to get an item value of json using C#?

json:

[{
    ID: '6512',
    fd: [{
        titie: 'Graph-01',
        type: 'graph',
        views: {
            graph: {
                show: true,
                state: {
                    group: 'DivisionName',
                    series: ['FieldWeight', 'FactoryWeight', 'Variance'],
                    graphType: 'lines-and-points'
                }
            }
        }
    }, {
        titie: 'Graph-02',
        type: 'Graph',
        views: {
            graph: {
                show: true,
                state: {
                    group: 'DivisionName',
                    series: ['FieldWeight', 'FactoryWeight', 'Variance'],
                    graphType: 'lines-and-points'
                }
            }
        }
    }]
}, {
    ID: '6506',
    fd: [{
        titie: 'Map-01',
        type: 'map',
        views: {
            map: {
                show: true,
                state: {
                    kpiField: 'P_BudgetAmount',
                    kpiSlabs: [{
                        id: 'P_BudgetAmount',
                        hues: ['#0fff03', '#eb0707'],
                        scales: '10'
                }]
                }
            }
        }
    }]
}]

Above mentioned one is json, Here titie value will be get in a list please help me...

My code is:

string dashletsConfigPath = Url.Content("~/Content/Dashlets/Dashlets.json");
string jArray = System.IO.File.ReadAllText(Server.MapPath(dashletsConfigPath));
List<string> lists = new List<string>();
JArray list = JArray.Parse(jArray);
var ll = list.Select(j => j["fd"]).ToList();

Here json will be converted in to JArray then li got fd details then we got tite details on a list

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
user3487837
  • 23
  • 1
  • 6

6 Answers6

0

I would recommend creating a class for your objects and use a DataContractSerializer to deserialize the JSON string.

[DataContract]
public class Container
{
  [DataMember(Name="ID")]
  public string ID { get; set; }

  [DataMember(Name="fd")]
  public Graph[] fd { get; set; }
}

[DataContract]
public class Graph
{
  [DataMember(Name="title")]
  public string Title { get; set; }

  [DataMember(Name="type")]
  public string Type { get; set; }
}

etc.

This will give you strongly typed classes around your JSON.

Scott Cramer
  • 304
  • 2
  • 3
0

I'm not sure how you intend to use the data but you can collect all the titie values from your objects like this:

var arr = JArray.Parse(json);
var query =
    from JObject obj in arr
    from JObject fd in obj["fd"]
    select new
    {
        Id = (string)obj["ID"],
        Titie = (string)fd["titie"],
    };
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
0

If you just want a List<string> of the "titie" (sic) property values, this should work, using SelectMany:

List<string> result = list.SelectMany(
    obj => obj["fd"]
        .Select(inner => inner["titie"].Value<string>()))
    .ToList()

This assumes that the JSON you posted is made valid by quoting property names.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

You can use json.net to deserialize json string like this:

public class Item
{
    public string ID { get; set; }
    public List<FD> fd { get; set; }
}

public class FD
{
    public string titie { get; set; }
    public string type { get; set; }
    public Views views { get; set; }
}

public class Views
{
    public Graph graph { get; set; }
}

public class Graph
{
    public bool show { get; set; }
    public State state { get; set; }
}

public class State
{
    public string group { get; set; }
    public string[] series { get; set; }
    public string graphType { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string json = @"..."; //your json string

        var Items = JsonConvert.DeserializeObject<List<Item>>(json);

        List<string> tities = new List<string>();
        foreach (var Item in Items)
        {
            foreach (var fd in Item.fd)
            {
                tities.Add(fd.titie);
            }
        }

    }
}
ArMaN
  • 2,306
  • 4
  • 33
  • 55
0

First thing is your json format invalid. get the valid json below.

[{ "ID": "6512", "fd": [{ "titie": "Graph-01", "type": "graph", "views": { "graph": { "show": true, "state": { "group": "DivisionName", "series": ["FieldWeight", "FactoryWeight", "Variance"], "graphType": "lines-and-points" } } } }, { "titie": "Graph-02", "type": "Graph", "views": { "graph": { "show": true, "state": { "group": "DivisionName", "series": ["FieldWeight", "FactoryWeight", "Variance"], "graphType": "lines-and-points" } } } }] }, { "ID": "6506", "fd": [{ "titie": "Map-01", "type": "map", "views": { "map": { "show": true, "state": { "kpiField": "P_BudgetAmount", "kpiSlabs": [{ "id": "P_BudgetAmount", "hues": ["#0fff03", "#eb0707"], "scales": "10" }] } } } }] }]

And if you want to read items as separate strings use following code.

JArray jObject = JArray.Parse(json);

var ll = jObject.ToList();
Seminda
  • 1,745
  • 12
  • 15
0

You can use Newtonsoft Json Library.

Deserialize your json string with the model objects below

   public class JsonWrapper
    {
        public string ID { get; set; }
        public List<Fd> fd { get; set; }
    }

 public class Fd
    {
        public string titie { get; set; }
        public string type { get; set; }
        public Views views { get; set; }
    }

public class Views
    {
        public Graph graph { get; set; }
    }

public class Graph
    {
        public bool show { get; set; }
        public State state { get; set; }
    }

 public class State
    {
        public string group { get; set; }
        public List<string> series { get; set; }
        public string graphType { get; set; }
    }

Declare on object of JsonWrapper class and deserialize the json string to it.

JsonWrapper jsonWrapper = new JsonWrapper();
jsonWrapper = (JsonWrapper)JsonConvert.DeserializeObject(jsonString, jsonWrapper.getType());

Then you can access all the lists and properties from the jsonWrapper object.

Mobility
  • 263
  • 2
  • 5