【问题标题】:Parse unformatted string into NSDictionary将未格式化的字符串解析为 NSDictionary
【发布时间】:2014-05-16 01:25:52
【问题描述】:

我正在调用一个服务器 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 并获取我需要的数据。

有没有更好的方法?任何指针将不胜感激。

谢谢!

【问题讨论】:

  • 这是完整的回复吗?它看起来不完整,请完全按照您的要求编写响应的 NSLOG。
  • 为什么不正常解析JSON???
  • 这确实是NSLog打印出来的完整响应
  • @HotLicks - 这不是一个有效的 json
  • 啊,我明白了,它是糟糕的 JSON。哪一头搞砸了??

标签: ios objective-c json nsstring nsdictionary


【解决方案1】:

有多种方法,您可以根据自己的喜好选择最舒适的方法和最熟悉的方法,以及性能和内存要求。

一种方法是编写一个小适配器,将服务器响应作为输入并为您提供有效的 JSON 结果(因为输入非常接近),然后使用现成的 JSON 解析器转换为字典(如 @ 所述987654321@)。特别是如果您真正需要的是可以最快实施的站数据 - json 化将只是一系列简单的字符串替换,如here 所述。即用@"\"stnCode\"" 替换@"stnCode",依此类推。

如果您最终使用字符串搜索函数自己解析它,我肯定会专注于将表示站时间的每条线的解析作为一种单一的方法,在下一个“{stnCode”的位置通过后续"}" - 或者如果你知道你会得到换行符,你可以逐行处理。

第

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2021-09-17
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多