2016-07-26 111 views
0

我有一個模型,看起來像:遍歷集合並創建一個嵌套的對象

public class SearchResult 
{ 
    public bool success { get; set; } 
    public SearchData data { get; set; } 
} 

public class SearchData 
{ 
    public string UploadDate { get; set; } 
    public List<UserImages> Images { get; set; } 
} 

public class UserImages 
{ 
    public string Filename { get; set; } 
    public string FileId { get; set; } 
} 

我收藏在下面的格式返回結果

FileId FileName UploadDate 
148847 IMG_1.JPG Mar-2012 
135710 IMG_8.JPG Mar-2012 
143817 IMG_6.JPG Jul-2013 
143873 IMG_5.JPG Aug-2014 
145766 IMG_4.JPG Aug-2015 
145820 IMG_3.JPG Jan-2016 
145952 IMG_2.JPG Jan-2016 

我想要序列上述集合,以便我可以生成以下JSON:

{ 
    "success": true, 
    "SearchData": { 
     "UploadDate": "MAR-2012", 
     "UserImages": [{ 
      "Filename": "IMG_1.JPG", 
      "FileId ": "148847" 
     }, { 
      "Filename": "IMG_8.JPG", 
      "FileId ": "135710" 
     }], 
     "UploadDate": "Jul-2013", 
     "UserImages": [{ 
      "Filename": "IMG_6.JPG", 
      "FileId ": "143817" 
     }] 
    } 
} 

我只是無法獲得迭代工作。我如何迭代集合來創建我的模型類的對象,以便以後序列化它?

+1

哪裏是你的代碼來創建JSON? – 2016-07-26 13:59:13

+0

通過json2csharp.com反向工程你的JSON - 看起來你的'FileId'將是一個無效的屬性名稱 - 你可以嘗試改變它到別的,然後再次序列化你的JSON?你用什麼來序列化到JSON? –

+0

我正在使用ASP.Net Web API,它將以JSON格式返回所有內容。所以,需要一個根對象,並且控制器方法會將其隱式序列化爲JSON。 – user1640256

回答

2

這會將您的集合轉換爲可以序列化爲JSON的對象模型。

var result = new SearchResult 
{ 
    Success = true, 
    SearchData = results.GroupBy(item => item.UploadDate, 
       (key, grouping) => new SearchData 
       { 
        UploadDate = key, 
        Images = grouping.Select(item => new UserImage 
        { 
         FileName = item.FileName, 
         FileId = item.FileId 
        }).ToList() 
       }).ToList() 
}; 

var json = new JavaScriptSerializer().Serialize(result); 
var json_otherWay = Newtonsoft.Json.JsonConvert.SerializeObject(result); 

注意雖然這SearchData是一個集合而不是一個單一的項目如上面代碼:

public class SearchResult 
{ 
    public bool Success { get; set; } 
    public List<SearchData> Data { get; set; } 
} 

的JSON一部分,我從here

+0

我在新的SearchResult類定義中添加了,以清楚(er)它需要修改。 – RoadieRich

+0

@RoadieRich - 如果它解決了你的問題,請考慮標記問題爲答案:) –

+0

它的確如此。謝謝。 – user1640256

0

試試這個。

using Newtonsoft.Json; 

public class SearchData 
{ 
    public string UploadDate { get; set; } 
    public List<UserImages> Images { get; set; } 
    public bool success { get; set; } 
} 

public class UserImages 
{ 
    public string Filename { get; set; } 
    public string FileId { get; set; } 
} 

var result= JsonConvert.DeserializeObject<List<SearchData>>("JsonString") 
1

這聽起來像一個典型的組通過了題。這是一個自包含的示例,應該將編譯複製粘貼到任何測試類中。

public class SearchResult 
{ 
    public bool success { get; set; } 
    public SearchData data { get; set; } 
} 

public class SearchData 
{ 
    public string UploadDate { get; set; } 
    public IEnumerable<UserImages> Images { get; set; } 
} 

public class UserImages 
{ 
    public string Filename { get; set; } 
    public string FileId { get; set; } 
} 

public class FlatData 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public DateTime Date { get; set; } 
} 

public static void Test() 
{ 
    //generate arbitrary dates to group on 
    var dates = Enumerable.Range(0, 3).Select(x => DateTime.Today.AddDays(x)).ToArray(); 
    //generate some sample data in the flat format 
    var flatData = Enumerable.Range(1, 10).Select(x => new FlatData() { Id = x.ToString(), Name = "Image_" + x, Date = dates[x % 3] }); 

    //group the flat data into the hierarchical format 
    var grouped = from item in flatData 
        group item by item.Date into g 
        select new SearchData() 
        { 
         UploadDate = g.Key.ToShortDateString(), 
         Images = from img in g 
           select new UserImages() 
           { 
            FileId = img.Id, 
            Filename = img.Name 
           } 
        }; 

    //Serialization implementation abstracted, as it should be 
    var json = Common.Helper.SerializeJSON(grouped); 
}} 

樣品JSON輸出:

[ 
    { 
     "UploadDate":"7/27/2016", 
     "Images":[ 
     { 
      "Filename":"Image_1", 
      "FileId":"1" 
     }, 
     { 
      "Filename":"Image_4", 
      "FileId":"4" 
     }, 
     { 
      "Filename":"Image_7", 
      "FileId":"7" 
     }, 
     { 
      "Filename":"Image_10", 
      "FileId":"10" 
     } 
     ] 
    }, 
    { 
     "UploadDate":"7/28/2016", 
     "Images":[ 
     { 
      "Filename":"Image_2", 
      "FileId":"2" 
     }, 
     { 
      "Filename":"Image_5", 
      "FileId":"5" 
     }, 
     { 
      "Filename":"Image_8", 
      "FileId":"8" 
     } 
     ] 
    }, 
    { 
     "UploadDate":"7/26/2016", 
     "Images":[ 
     { 
      "Filename":"Image_3", 
      "FileId":"3" 
     }, 
     { 
      "Filename":"Image_6", 
      "FileId":"6" 
     }, 
     { 
      "Filename":"Image_9", 
      "FileId":"9" 
     } 
     ] 
    } 
]