我有一个模型,看起来像:
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"
}]
}
}
我就是无法让迭代工作。我如何迭代集合以创建模型类的对象,以便稍后对其进行序列化?
这将把您的集合变成对象模型,然后您可以将其序列化为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部分
这听起来像是一个经典的问题组。这里有一个自包含的示例,应该将粘贴编译复制到任何测试类中。
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"
}
]
}
]
试试这个。
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")