我正在调用服务器api,它给了我以下响应。
_variable_1400202371156={
trainDataFound:"trainRunningDataFound",trainNo:"14674",trainName:"SHAHEED EXP",from:"ASR",to:"JYG",schArrTime:"01:30",schDepTime:"11:55",dayCnt:2,idTrainDef:"48380",runsOn:"1010110",runsOnDays:getDaysOfRunString("1010110"),trainType:"MAIL_EXP",
prfValid:true, prfFlag:"0",vldFrm:"15 Jul 2013",vldTo:"31 Dec 2099",
trnName:function(){return _LANG=="en-us"?"SHAHEED EXP":"शहीदएक्स."},
trainSchedule:{
stations:[
{stnCode:"ASR",arrTime:"00:00",depTime:"11:55",dayCnt:0,distance:0} ,
{stnCode:"BEAS",arrTime:"12:23",depTime:"12:25",dayCnt:0,distance:42} ,
{stnCode:"JUC",arrTime:"13:05",depTime:"13:10",dayCnt:0,distance:78} ,
{stnCode:"JRC",arrTime:"13:21",depTime:"13:23",dayCnt:0,distance:83} ,
{stnCode:"PGW",arrTime:"13:38",depTime:"13:40",dayCnt:0,distance:99} ,
{stnCode:"PHR",arrTime:"13:58",depTime:"14:00",dayCnt:0,distance:122} ,
{stnCode:"LDH",arrTime:"14:15",depTime:"14:25",dayCnt:0,distance:135} ,
{stnCode:"DOA",arrTime:"15:01",depTime:"15:02",dayCnt:0,distance:157} ,
{stnCode:"KNN",arrTime:"15:18",depTime:"15:19",dayCnt:0,distance:177} ,
{stnCode:"GVG",arrTime:"15:27",depTime:"15:29",dayCnt:0,distance:186} ,
{stnCode:"SIR",arrTime:"15:40",depTime:"15:42",dayCnt:0,distance:195} ,
{stnCode:"RPJ",arrTime:"16:00",depTime:"16:01",dayCnt:0,distance:221} ,
{stnCode:"UBC",arrTime:"16:22",depTime:"16:24",dayCnt:0,distance:241} ,
{stnCode:"UMB",arrTime:"17:00",depTime:"17:10",dayCnt:0,distance:249} ,
{stnCode:"KKDE",arrTime:"17:47",depTime:"17:49",dayCnt:0,distance:290} ,
{stnCode:"KUN",arrTime:"18:16",depTime:"18:18",dayCnt:0,distance:323} ,
{stnCode:"PNP",arrTime:"18:49",depTime:"18:51",dayCnt:0,distance:358} ,
{stnCode:"SNP",arrTime:"19:25",depTime:"19:27",dayCnt:0,distance:403} ,
{stnCode:"SZM",arrTime:"20:13",depTime:"20:15",dayCnt:0,distance:443} ,
{stnCode:"DLI",arrTime:"20:45",depTime:"21:15",dayCnt:0,distance:446} ,
{stnCode:"GZB",arrTime:"22:05",depTime:"22:07",dayCnt:0,distance:466} ,
{stnCode:"HPU",arrTime:"22:45",depTime:"22:48",dayCnt:0,distance:503} ,
{stnCode:"AMRO",arrTime:"23:48",depTime:"23:50",dayCnt:0,distance:576} ,
{stnCode:"MB",arrTime:"00:35",depTime:"00:45",dayCnt:1,distance:607} ,
{stnCode:"RMU",arrTime:"01:15",depTime:"01:17",dayCnt:1,distance:634} ,
{stnCode:"BE",arrTime:"02:15",depTime:"02:20",dayCnt:1,distance:697} ,
{stnCode:"SPN",arrTime:"03:25",depTime:"03:30",dayCnt:1,distance:768} ,
{stnCode:"LKO",arrTime:"06:10",depTime:"06:40",dayCnt:1,distance:932} ,
{stnCode:"BBK",arrTime:"07:23",depTime:"07:25",dayCnt:1,distance:961} ,
{stnCode:"GD",arrTime:"08:45",depTime:"08:55",dayCnt:1,distance:1049} ,
{stnCode:"MUR",arrTime:"09:18",depTime:"09:20",dayCnt:1,distance:1077} ,
{stnCode:"BV",arrTime:"09:44",depTime:"09:46",dayCnt:1,distance:1108} ,
{stnCode:"BST",arrTime:"10:15",depTime:"10:20",dayCnt:1,distance:1138} ,
{stnCode:"KLD",arrTime:"10:43",depTime:"10:45",dayCnt:1,distance:1168} ,
{stnCode:"GKP",arrTime:"11:55",depTime:"12:15",dayCnt:1,distance:1203} ,
{stnCode:"DEOS",arrTime:"12:58",depTime:"13:00",dayCnt:1,distance:1252} ,
{stnCode:"BTT",arrTime:"13:25",depTime:"13:30",dayCnt:1,distance:1273} ,
{stnCode:"BHTR",arrTime:"13:41",depTime:"13:43",dayCnt:1,distance:1286} ,
{stnCode:"MW",arrTime:"14:08",depTime:"14:10",dayCnt:1,distance:1301} ,
{stnCode:"SV",arrTime:"14:50",depTime:"14:55",dayCnt:1,distance:1322} ,
{stnCode:"CPR",arrTime:"16:40",depTime:"17:00",dayCnt:1,distance:1383} ,
{stnCode:"SEE",arrTime:"17:57",depTime:"17:59",dayCnt:1,distance:1436} ,
{stnCode:"HJP",arrTime:"18:09",depTime:"18:14",dayCnt:1,distance:1442} ,
{stnCode:"MFP",arrTime:"19:25",depTime:"19:35",dayCnt:1,distance:1496} ,
{stnCode:"SPJ",arrTime:"21:05",depTime:"21:35",dayCnt:1,distance:1547} ,
{stnCode:"HYT",arrTime:"21:58",depTime:"22:00",dayCnt:1,distance:1569} ,
{stnCode:"LSI",arrTime:"22:25",depTime:"22:30",dayCnt:1,distance:1580} ,
{stnCode:"DBG",arrTime:"22:40",depTime:"22:50",dayCnt:1,distance:1585} ,
{stnCode:"KKHT",arrTime:"23:04",depTime:"23:06",dayCnt:1,distance:1592} ,
{stnCode:"TRS",arrTime:"23:15",depTime:"23:17",dayCnt:1,distance:1598} ,
{stnCode:"SKI",arrTime:"23:30",depTime:"23:35",dayCnt:1,distance:1604} ,
{stnCode:"PDW",arrTime:"23:47",depTime:"23:49",dayCnt:1,distance:1612} ,
{stnCode:"MBI",arrTime:"00:12",depTime:"00:18",dayCnt:2,distance:1621} ,
{stnCode:"RJA",arrTime:"00:31",depTime:"00:33",dayCnt:2,distance:1631} ,
{stnCode:"KJI",arrTime:"00:48",depTime:"00:50",dayCnt:2,distance:1640} ,
{stnCode:"JYG",arrTime:"01:30",depTime:"00:00",dayCnt:2,distance:1653}
]
}
}
我想了解这个响应,最好将其转换为NSDictionary。解决这个问题的最佳方法是什么?我无法更改服务器代码以返回有效的json,xml,因此这是不可能的。
我能想到解析响应的另一种方法是使用NSString substring apis并获取我需要的数据。
还有更好的方法吗?任何指针都会受到赞赏。
谢谢!
答案 0 :(得分:0)
有几种方式,你真正应对的是你最舒服的追求和方式。这似乎是最熟悉的,也是性能和内存要求。
一种方法是编写一个小的适配器,它将服务器响应作为输入,并为您提供有效的JSON结果(因为输入非常接近),然后使用现成的JSON解析器转换为字典(如上所述{ {3}})。特别是如果您真正需要的是可以最快实现的站点数据 - jsonification只是一系列简单的字符串替换问题,如here所述。即替换@" stnCode"与@" \" stnCode \""等等。
如果您最终使用字符串搜索功能自行解析它,我绝对会专注于将表示站点时间的每一行解析为一个单一的方法,该方法在下一个位置进行拾取" { stnCode"通过随后的"}" - 或者如果你知道你会得到换行符,你可以逐行处理。
hth