如何使用c#获取json的项值



如何使用c#获得json的项值?

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'
                }]
                }
            }
        }
    }]
}]

上面提到的一个是json,这里的值将在一个列表中得到请帮帮我……

我的代码是:
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();

这里json将被转换为JArrayLi得到了fd details然后我们得到了list上的details

如果你只想要一个"titie" (sic)属性值的List<string>,这应该工作,使用SelectMany:

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

这假定您发布的JSON通过引用属性名而有效。

我建议为您的对象创建一个类,并使用DataContractSerializer来反序列化JSON字符串。

[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; }
}

等。

我不确定你打算如何使用数据,但你可以收集所有的titie值从你的对象,像这样:

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"],
    };

你可以使用json.net来反序列化json字符串,像这样:

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);
            }
        }
    }
}

首先是你的json格式无效。获取下面的有效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" }] } } } }] }]

如果您想将项作为单独的字符串读取,请使用以下代码:

JArray jObject = JArray.Parse(json);
var ll = jObject.ToList();

您可以使用Newtonsoft Json Library

用下面的模型对象反序列化json字符串

   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; }
    }

声明一个JsonWrapper类的对象,并将json字符串反序列化到它。

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

然后您可以访问jsonWrapper对象中的所有列表和属性。

相关内容

  • 没有找到相关文章

最新更新