2014-01-23 90 views
1

我有幾個小時卡在這個,我不知道爲什麼我失去了我的JSON,與失去我的意思是它有'結構',但不是值。我試圖用Newtonsoft .Json庫的JObjects返回一個JArray。這是一個簡單的例子,我試圖用Linq做到這一點,但獲得相同的結果。建設Json Newtonsoft Jarray

我使用ASP的MVC,我把this例如

我的控制器:

JArray jsonTest = new JArray(
       new JObject 
        { 
         {"Title", "hello"}, 
         { 
         "Author", new JObject 
          { 
          {"Name", "hello"}, 
          {"Twitter", "hello"} 
          } 
         }, 
         {"Date", "hello"}, 
         {"BodyHtml", "hello"}, 
        }, 
        new JObject 
        { 
         {"Title", "hello"}, 
         { 
         "Author", new JObject 
          { 
          {"Name", "hello"}, 
          {"Twitter", "hello"} 
          } 
         }, 
         {"Date", "hello"}, 
         {"BodyHtml", "hello"}, 
        } 
      ); 

     return Json(jsonTest,JsonRequestBehavior.AllowGet); 

,我也得到:

enter image description here

,你可以看到我的結構是存在的( 2個對象,第二個對象是一個包含2個元素的數組),但是我沒有數據。

我試過jsonTest.tostring(),但我得到了我的字符串中每個字符的數組。

我錯過了什麼?

回答

2

兩個JObjects添加到Jarray

 
JObject obj1=new JObject(); 

JObject obj2=new JObject(); 

JArray arr=new JArray(); 

    arr.Add(obj1); 
    arr.Add(obj2); 

    return arr.ToString(Newtonsoft.Json.Formatting.Indented);