2017-06-01 19 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); 
} 
相關問題