我有下面的JSON。反序列化时,responseHeader
和response
可以正常工作,但highlighting
不能正常工作。
问题正在突出显示数据,如下所示,_text_
为空。我想将两个值都存储在两个字段中。问题是第一部分没有任何常量字段名称。
1. C:\\TestArea..........
2. _text_
"highlighting":{
"C:\\TestArea\\Destination\\SUP000011\\ATM-1B4L2KQ0ZE0-0001\\SoS_Update_2018_06_04_pram.pptx":{
"_text_":["\nSCRUM of SCRUMs S4-W1 \n AXP Internal \n 4-<em>Jun</em>-18 \n \n slide-master-content \n \n \n slide-notes \n 4-<em>Jun</em>"]},
JSON:
{
"responseHeader": {
"status": 0,
"QTime": 115,
"params": {
"q": "\"John\"",
"hl": "on",
"hl.simple.post": "</em>",
"start": "0",
"rows": "10000",
"hl.simple.pre": "<em>"
}
},
"response": {
"numFound": 10,
"start": 0,
"docs": [{
"domain": ["FIU/FCRU"],
"id": "C:\\TestArea\\Destination\\SUP000011\\ATM-1B4L2KQ0ZE0-0001\\SoS_Update_2018_06_04_pram.pptx"
}]
},
"highlighting": {
"C:\\TestArea\\Destination\\SUP000011\\ATM-1B4L2KQ0ZE0-0001\\SoS_Update_2018_06_04_pram.pptx": {
"_text_": ["\nSCRUM of SCRUMs S4-W1 \n AXP Internal \n 4-<em>Jun</em>-18 \n \n slide-master-content \n \n \n slide-notes \n 4-<em>Jun</em>"]
},
"C:\\TestArea\\Destination\\SUP000005\\F-3-20150505-0028\\tt.csv": {
"_text_": [",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL\r\n930,<em>John</em> Ferguson,NULL,2015-06-30 15:27:04.677,test.test"]
}
}
}
我的数据模型和代码:
public class RootObject
{
public ResponseHeader responseHeader { get; set; }
public Response response { get; set; }
public Highlighting highlighting { get; set; }
}
public class Highlighting
{
public List<string> _text_ { get; set; }
}
var outObject = JsonConvert.DeserializeObject<RootObject>(jsonString, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
答案 0 :(得分:2)
对于string(from:to:)
属性,使用import Foundation
let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.allowedUnits = [.year, .month, .day]
let string = formatter.string(from: startDate, to: endDate)!
print(string) // prints: 4 years, 5 months, 9 days
就像这样:
@IBAction func middleFinderButton(_ sender: Any) {
let totalLatitude = mapView.annotations.reduce(0) { $0 + $1.coordinate.latitude }
let totalLongitude = mapView.annotations.reduce(0) { $0 + $1.coordinate.longitude }
let averageLatitude = totalLatitude/Double(mapView.annotations.count)
let averageLongitude = totalLongitude/Double(mapView.annotations.count)
let centerPoint = MKPointAnnotation()
centerPoint.coordinate.latitude = averageLatitude
centerPoint.coordinate.longitude = averageLongitude
mapView.addAnnotation(centerPoint)
}
为什么这样做?如果我像这样简化highlighting
的值:
Dictionary<string, Highlighting>
我们可以看到它是一个JSON对象,其变量属性名称的值具有固定的模式。 Json.NET支持在字典之间进行序列化。有关详细信息,请参见Deserialize a Dictionary。
提琴here。