簡體   English   中英

將未格式化的字符串解析為NSDictionary

[英]Parse unformatted string into NSDictionary

我正在調用服務器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子字符串api並獲取我需要的數據。

有什么更好的辦法嗎? 任何指針將不勝感激。

謝謝!

有幾種方法,這取決於您,您將最自如地追求&似乎最熟悉,以及性能和內存要求。

一種方式是寫一個小適配器都在服務器的響應作為輸入,並為您提供了一個有效的JSON結果(因為輸入非常接近),然后使用現成的JSON解析器將轉換為一個字典(如描述在這里 )。 特別是如果你真正需要的是,可能是最快實現站的數據-在jsonification將只是一系列簡單的字符串替換所描述的問題在這里 即用@“ \\” stnCode \\“”替換@“ stnCode”,依此類推。

如果最終自己使用字符串搜索功能進行了解析,那么我肯定會集中精力進行代表站時間的每一行的解析,將其作為一站式方法,從下一個“ {stnCode”到后續的“}”的位置進行解析。 -或者,如果您知道要換行,則可以逐行處理。

hth

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM