I am calling a server api that gives me the following response.

_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} 
        ]
    }
}

I want to make sense of this response, preferably convert it to an NSDictionary. What would be the best way of approaching this problem? I cant change the server code to return a valid json, xml so that is out of question.

Another way I can think of parsing the response is using the NSString substring apis and get the data I need.

Is there any better way? Any pointers would be appreciated.

Thanks!

没有正确的解决方案

其他提示

There are several ways and it's really up to you which you'll be most comfortable pursuing & which seems most familiar, and also performance and memory requirements.

One way is to write a little adapter that takes the server response as input and gives you a valid JSON result (since the input very close), and then use ready-made JSON parser to convert to a dictionary (as described here). Especially if all you really need is the station data that could be quickest to implement - the jsonification would just be a matter of a series of simple string substitutions as described here. I.e replace @"stnCode" with @"\"stnCode\"", and so on.

If you end up parsing it yourself using string search functions I'd definitely focus making the parsing of each line representing station times as a single-minded method that picks up at the location of the next "{stnCode" through the subsequent "}" - or if you know you'll get line breaks you can just process line by line.

hth

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top