2017-06-01 44 views
0

如何从这个JSON字符串创建使用Newtonsoft.JSON(json.net)JSON数组如何使用Newtonsoft.Json这种特殊结构

[ 
    { 
    "Cells": { 
     "results": [ 
     { 
      "Key": "Title", 
      "Value": "hello", 
      "ValueType": "Edm.String" 
     }, 
     { 
      "Key": "Size", 
      "Value": "54549", 
      "ValueType": "Edm.Int64" 
     }, 
     { 
      "Key": "Path", 
      "Value": "http://somesite/a/hello.pptx", 
      "ValueType": "Edm.String" 
     }, 
     { 
      "Key": "Summary", 
      "Value": "Some summary <ddd/> interesting reading <ddd/> nice book <ddd/> ", 
      "ValueType": "Edm.String" 
     }, 
     { 
      "Key": "Name", 
      "Value": "http://somesite", 
      "ValueType": "Edm.String" 
     } 
     ] 
    } 
    }, 
    { 
    "Cells": { 
     "results": [ 
     { 
      "Key": "Title", 
      "Value": "hi joe", 
      "ValueType": "Edm.String" 
     }, 
     { 
      "Key": "Size", 
      "Value": "41234", 
      "ValueType": "Edm.Int64" 
     }, 
     { 
      "Key": "Path", 
      "Value": "http://someothersite/interesting/hi.pptx", 
      "ValueType": "Edm.String" 
     }, 
     { 
      "Key": "Summary", 
      "Value": "Some summary <ddd/> interesting reading <ddd/> nice book <ddd/> ", 
      "ValueType": "Edm.String" 
     }, 
     { 
      "Key": "Name", 
      "Value": "http://somesite", 
      "ValueType": "Edm.String" 
     } 
     ] 
    } 
    } 
] 

json2csharp给了我下面的类此创建JSON数组结构

public class Result 
{ 
    public string Key { get; set; } 
    public string Value { get; set; } 
    public string ValueType { get; set; } 
} 

public class Cells 
{ 
    public List<Result> results { get; set; } 
} 

public class RootObject 
{ 
    public Cells Cells { get; set; } 
} 

如何使用这些类来创建json数组?

更新和解决方案

这将工作

static void Main(string[] args) 
{ 
    RootObject ro = new RootObject(); 
    Cells cs = new Cells(); 
    cs.results = new List<Result>(); 

    Result rt = new Result(); 
    rt.Key = "Title"; 
    rt.Value = "hello"; 
    rt.ValueType = "Edm.String"; 
    cs.results.Add(rt); 

    Result rs = new Result(); 
    rs.Key = "Size"; 
    rs.Value = "3223"; 
    rs.ValueType = "Edm.Int64"; 
    cs.results.Add(rs); 

    ro.Cells = cs; 

    string json = JsonConvert.SerializeObject(ro); 
} 
+0

理解了它,见上面的解决方案。谢谢 – pixel

+2

而不是在你的问题中包括答案,你可以[回答你自己的问题](https://stackoverflow.com/help/self-answer)并接受答案,以便我们其他人可以告诉你的问题已解决。 – dbc

+0

谢谢,更新回答 – pixel

回答

1

您正在寻找的功能DeserializeObject<T>:给定一个POCO的下面一个例子

var json = ""; // string up above in your code 
var jObect = JsonConvert.DeserializeObject<RootObject>(json); 

// Use 

var cells = jObject.Cells; 
var result1 = cells.results.FirstOrDefault(); 
+0

谢谢,但我需要从我的对象中产生json字符串,请参阅上面的更新。 – pixel

+0

@pixel向相反方向行进是'var jsonStr = JsonConvert.SerializeObject(myObject)'。 – Kyle

+0

其实,我接下来需要的是将其转换回JArray而不是jsonString :) – pixel

0

public class Account 
{ 
    public string Email { get; set; } 
    public bool Active { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public IList<string> Roles { get; set; } 
} 
下面

string json = @"{ 
    'Email': '[email protected]', 
    'Active': true, 
    'CreatedDate': '2013-01-20T00:00:00Z', 
    'Roles': [ 
    'User', 
    'Admin' 
    ] 
}"; 

Account account = JsonConvert.DeserializeObject<Account>(json); 

Console.WriteLine(account.Email); 

参考Newtonsoft的文档:

这可以通过反序列化下面的JSON字符串显示实现 http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

+0

谢谢,我不想使用json字符串。我在上面提供了json字符串来显示结构。 – pixel

0

如果你想要一个对象的字符串表示,尤其是一个JSON对象,最相关的是.ToString()。 但是,它可能由于其他原因失败...

1

这将工作

static void Main(string[] args) 
{ 
    RootObject ro = new RootObject(); 
    Cells cs = new Cells(); 
    cs.results = new List<Result>(); 

    Result rt = new Result(); 
    rt.Key = "Title"; 
    rt.Value = "hello"; 
    rt.ValueType = "Edm.String"; 
    cs.results.Add(rt); 

    Result rs = new Result(); 
    rs.Key = "Size"; 
    rs.Value = "3223"; 
    rs.ValueType = "Edm.Int64"; 
    cs.results.Add(rs); 

    ro.Cells = cs; 

    string json = JsonConvert.SerializeObject(ro); 
} 
相关问题