在iPhone中拆分JSON数据

时间:2012-03-14 07:55:51

标签: iphone string json decode

我使用JSON从我的服务器获取了这些数据:

{
    ID = 198;
    dtDate = "2012-03-14 00:00:00";
    dtTime = "06:00:00";
    iPublished = 1;
    sProgram = "Devotional Hits";
}
{
    ID = 199;
    dtDate = "2012-03-14 00:00:00";
    dtTime = "07:00:00";
    iPublished = 1;
    sProgram = "Old Malayalam Hits";
    }
{
    ID = 200;
    dtDate = "2012-03-14 00:00:00";
    dtTime = "08:00:00";
    iPublished = 1;
    sProgram = "Malayalam New Hits";
}
{
    ID = 201;
    dtDate = "2012-03-14 00:00:00";
    dtTime = "09:00:00";
    iPublished = 1;
    sProgram = "Melody Songs";
}
{
    ID = 202;
    dtDate = "2012-03-14 00:00:00";
    dtTime = "10:00:00";
    iPublished = 1;
    sProgram = "Jayachandran Hits";
}
{
    ID = 203;
    dtDate = "2012-03-14 00:00:00";
   dtTime = "11:00:00";
    iPublished = 1;
    sProgram = "Yesudas Hits";
 }
 {
    ID = 204;
    dtDate = "2012-03-14 00:00:00";
    dtTime = "12:00:00";
    iPublished = 1;
    sProgram = "Ilayaraja Hits";
  }

我需要拆分这些数据并将sProgram数据存储到数组中..需要帮助。

我需要拆分此格式,我想分别sProgramdtTime。我该怎么办呢。我对字符串格式有点困惑。

3 个答案:

答案 0 :(得分:2)

如果您正在使用< IOS 5,将SBJSON添加到您的项目中

//in some.m file

#import JSON.h

// Lets say NSString *recievedValue contains your JSON response.

id jsonRep = [receivedValue jsonValue];
if([jsonRep isKindOfClass:[NSArray class]])
{
   // returned JSON Value has a array structure.
   NSArray *value = (NSArray *)jsonRep;
   //You can access values using objectAtIndex: method if you already know the Index of a value, in your case i think each array object is a NSDictionary
   if([[value objectAtIndex:0] isKindOfClass:[NSDictionary class]])
   {
     // returned JSON Value has a key value compliant structure.
     NSDictionary *dicValue = (NSDictionary *)[value objectAtIndex:0];
     //You can access values using objectForKey: method if you already know the key value, in your case it can be @"sProgram" for example
   }
}
else if([jsonRep isKindOfClass:[NSDictionary class]])
{
   // returned JSON Value has a key value compliant structure.
   NSDictionary *value = (NSDictionary *)jsonRep;
   //You can access values using objectForKey: method if you already know the key value, in your case it can be @"sProgram" for example
}

答案 1 :(得分:1)

您的JSON无效 - 我建议您在将来发布与JSON相关的问题之前使用this tool进行检查。

但是,我已经更正了您的JSON并对其进行了验证 - 它应该如下所示:

[
    {
        "ID": 198,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "06:00:00",
        "iPublished": 1,
        "sProgram": "Devotional Hits"
    },
    {
        "ID": 199,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "07:00:00",
        "iPublished": 1,
        "sProgram": "Old Malayalam Hits"
    },
    {
        "ID": 200,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "08:00:00",
        "iPublished": 1,
        "sProgram": "Malayalam New Hits"
    },
    {
        "ID": 201,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "09:00:00",
        "iPublished": 1,
        "sProgram": "Melody Songs"
    },
    {
        "ID": 202,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "10:00:00",
        "iPublished": 1,
        "sProgram": "Jayachandran Hits"
    },
    {
        "ID": 203,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "11:00:00",
        "iPublished": 1,
        "sProgram": "Yesudas Hits"
    },
    {
        "ID": 204,
        "dtDate": "2012-03-14 00:00:00",
        "dtTime": "12:00:00",
        "iPublished": 1,
        "sProgram": "Ilayaraja Hits"
    }
]

然后,您可以使用json-framework对其进行解析。

正如您所问,现在您可以获得sProgram的所有值:

NSMutableArray *programs = [[NSMutableArray alloc] init];
NSArray *data = [json jsonValue];
for (NSDictionary *dict in data) {
    [programs addObject:[dict objectForKey:@"sProgram"]];
}
// programs now contains all the values of sProgram

答案 2 :(得分:0)

实际上我使用了Stig Brautaset的JSON库(2.2版)

- (void)viewDidLoad
{
[super viewDidLoad];
[table reloadData];
NSMutableData *responseData;
   responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL    URLWithString:@"http://ganamradio.com/smartphones/date.php"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];


// Do any additional setup after loading the view from its nib.
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}



- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}



 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//label.text = [NSString stringWithFormat:@"Connection failed: %@", [error   description]];
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];




   NSString *responseString = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding];
//[responseData release];

dict=[responseString JSONValue];

NSArray *Programs;
NSArray *Time;
Programs=[dict valueForKey:@"sProgram"];
Time=[dict valueForKey:@"dtTime"];

NSLog(@"......%@",Time);


}

所以我得到了NSArray程序中的程序列表,是他们使用这样的任何问题..?