`

iphone开发-------JSON解析

    博客分类:
  • ios
阅读更多
先看下整个json数据的格式,如下:
{
    "status": 200,
    "msg": "成功读取",
    "total": 4,
    "list": [
        {
            "rid": "100001",
            "name": "倪氏海泰",
            "sofc": "testdata",
            "pcc": "88",
            "tastegrade": "1",
            "envgrade": "2",
            "servgrade": "3",
            "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
            "image": "http://www.dianxiaor.com/dicpics/100001/100001.jpg",
            "address": "海泰大厦",
            "lng": "116.372684",
            "lat": "39.993992",
            "distance": "0",
            "discounts": [
                {
                    "did": "1",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/a.jpg"
                },
                {
                    "did": "0",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/b.jpg"
                },
                {
                    "did": "2",
                    "discount": "6",
                    "stime": "05:00:00",
                    "etime": "07:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/c.jpg"
                },
                {
                    "did": "0",
                    "discount": "6",
                    "stime": "05:00:00",
                    "etime": "07:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/d.jpg"
                },
                {
                    "did": "3",
                    "discount": "7",
                    "stime": "08:00:00",
                    "etime": "10:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/"
                },
                {
                    "did": "0",
                    "discount": "7",
                    "stime": "08:00:00",
                    "etime": "10:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/"
                },
                {
                    "did": "4",
                    "discount": "8",
                    "stime": "21:00:00",
                    "etime": "23:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/"
                },
                {
                    "did": "0",
                    "discount": "8",
                    "stime": "21:00:00",
                    "etime": "23:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100001big/"
                }
            ]
        },
        {
            "rid": "100002",
            "name": "大东北",
            "sofc": "testdata",
            "pcc": "88",
            "tastegrade": "1",
            "envgrade": "2",
            "servgrade": "3",
            "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
            "image": "http://www.dianxiaor.com/dicpics/100002/100002.jpg",
            "address": "志新东路",
            "lng": "116.375146",
            "lat": "39.994365",
            "distance": "213",
            "discounts": [
                {
                    "did": "1",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "02:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/a.jpg"
                },
                {
                    "did": "0",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "02:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/b.jpg"
                },
                {
                    "did": "2",
                    "discount": "6",
                    "stime": "03:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/c.jpg"
                },
                {
                    "did": "0",
                    "discount": "6",
                    "stime": "03:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/d.jpg"
                },
                {
                    "did": "3",
                    "discount": "7",
                    "stime": "08:00:00",
                    "etime": "10:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/"
                },
                {
                    "did": "0",
                    "discount": "7",
                    "stime": "08:00:00",
                    "etime": "10:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/"
                },
                {
                    "did": "4",
                    "discount": "8",
                    "stime": "21:00:00",
                    "etime": "23:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/"
                },
                {
                    "did": "0",
                    "discount": "8",
                    "stime": "21:00:00",
                    "etime": "23:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100002big/"
                }
            ]
        },
        {
            "rid": "100003",
            "name": "伊喜饺子",
            "sofc": "testdata",
            "pcc": "66",
            "tastegrade": "1",
            "envgrade": "2",
            "servgrade": "3",
            "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
            "image": "http://www.dianxiaor.com/dicpics/100003/100003.jpg",
            "address": "志新西路",
            "lng": "116.371552",
            "lat": "39.99612",
            "distance": "255",
            "discounts": [
                {
                    "did": "1",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/a.jpg"
                },
                {
                    "did": "0",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/b.jpg"
                },
                {
                    "did": "2",
                    "discount": "6",
                    "stime": "05:00:00",
                    "etime": "06:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/c.jpg"
                },
                {
                    "did": "0",
                    "discount": "6",
                    "stime": "05:00:00",
                    "etime": "06:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/d.jpg"
                },
                {
                    "did": "3",
                    "discount": "7",
                    "stime": "08:00:00",
                    "etime": "09:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/"
                },
                {
                    "did": "0",
                    "discount": "7",
                    "stime": "08:00:00",
                    "etime": "09:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/"
                },
                {
                    "did": "4",
                    "discount": "8",
                    "stime": "21:00:00",
                    "etime": "23:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/"
                },
                {
                    "did": "0",
                    "discount": "8",
                    "stime": "21:00:00",
                    "etime": "23:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100003big/"
                }
            ]
        },
        {
            "rid": "100004",
            "name": "小街卤煮",
            "sofc": "testdata",
            "pcc": "66",
            "tastegrade": "1",
            "envgrade": "2",
            "servgrade": "3",
            "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs",
            "image": "http://www.dianxiaor.com/dicpics/100004/100004.jpg",
            "address": "志新西路",
            "lng": "116.37272",
            "lat": "39.997516",
            "distance": "391",
            "discounts": [
                {
                    "did": "1",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/a.jpg"
                },
                {
                    "did": "0",
                    "discount": "5",
                    "stime": "01:00:00",
                    "etime": "04:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/b.jpg"
                },
                {
                    "did": "2",
                    "discount": "6",
                    "stime": "06:00:00",
                    "etime": "07:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/c.jpg"
                },
                {
                    "did": "0",
                    "discount": "6",
                    "stime": "06:00:00",
                    "etime": "07:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/d.jpg"
                },
                {
                    "did": "3",
                    "discount": "7",
                    "stime": "09:00:00",
                    "etime": "10:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/"
                },
                {
                    "did": "0",
                    "discount": "7",
                    "stime": "09:00:00",
                    "etime": "10:00:00",
                    "available": 10,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/"
                },
                {
                    "did": "4",
                    "discount": "8",
                    "stime": "00:00:00",
                    "etime": "00:00:00",
                    "available": 0,
                    "dimg": "http://www.dianxiaor.com/dicpics/100004big/"
                }
            ]
        }
    ]
}


工程结构图如下:
[img]

[/img]


1,先把SBJSON-Clssses这个文件夹导入工程里面


2,添加DataSource.h 与 DataSource.m到工程中
@interface DataSource : NSObject

//becarefull NSString is a object
+(id)dataSource:(NSString *)str;
+(id)dataSourceFromUrl:(NSString *)url;


@end


#import "JSON.h"
#import "DataSource.h"



@implementation DataSource


//解析本地json数据
+(id)dataSource:(NSString *)str
{
	//获取文件路径
	NSString * path = [[NSBundle mainBundle] pathForResource:str ofType:@""];
	
	//获取数据 
	NSData * reader = [NSData dataWithContentsOfFile:path];
    
	//转换成NSString
	NSString * JSONString = [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding];
	
	
	return [JSONString JSONValue];
}
//解析网络json数据,传入url
+(id)dataSourceFromUrl:(NSString *)url{


    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    //获取数据 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //转换成NSString
	NSString * JSONString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    return [JSONString JSONValue];








}



@end




3、调用,在ViewController.h添加如下代码:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
	NSDictionary * myData;
}


@property (retain, nonatomic) NSDictionary * myData;
@end





4、在在ViewController.m添加如下代码:
//
//  ViewController.m
//  JsonTestCao
//
//  Created by 张 志亮 on 13-1-3.
//  Copyright (c) 2013年 张 志亮. All rights reserved.
//

#import "ViewController.h"
#import "DataSource.h"
@interface ViewController ()

@end

@implementation ViewController
@synthesize myData;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //这是模拟操作json解析,用的是本地的json数据
    //获取数据源
//    NSString *str = @"jsondata";
//	myData = [DataSource dataSource:str];
//    NSInteger numbers = [[myData valueForKey:[[myData allKeys] objectAtIndex:0]] count];
//    NSString *sectionNanme = [[myData allKeys]objectAtIndex:1];
//    NSString *name = [[myData valueForKey:sectionNanme]objectAtIndex:0];
//    NSLog(@"%d",[myData count]);
//    NSLog(@"%d",numbers);
//    NSLog(@"%@",sectionNanme);
//    NSLog(@"%@",name);
    
    //获得url
    NSString *url = @"http://www.dianxiaor.com/getrinfos.php?lng=116.372684&lat=39.993995#jsonNav_l";
    //获得数据
    myData = [DataSource dataSourceFromUrl:url];
    NSLog(@"%d",[myData count]);
    //获得status
    NSString *status = [myData valueForKey:@"status"];
    NSLog(@"%@",status);
    //获得msg
    NSString *msg = [myData valueForKey:@"msg"];
    NSLog(@"%@",msg);
    
    //获得list的长度
    NSInteger countList = [[myData valueForKey:@"list"]count];
    NSLog(@"%d",countList);
    
    //获得rid
    NSString *rid = [[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"rid"];
    NSLog(@"%@",rid);
    
    //获得address
    NSString *address = [[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"address"];
    NSLog(@"%@",address);
    
    
    //获得discounts count  8
    NSInteger discounts = [[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]count];
    NSLog(@"discounts==%d",discounts);
    
    
    //获得list第一项里面discounts里面所有的dimg
    NSString *dimg = [[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]valueForKey:@"dimg"];
    //this is all dimg for "objectAtIndex:0" and "discounts"
    NSLog(@"dimg==%@",dimg);
    
    
    
    
    
    
    //获得list第一项里面discounts里面,一共有8项  第一项的dimg的值
    NSString *firstDimg = [[[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]objectAtIndex:0]valueForKey:@"dimg"];
    NSLog(@"firstDimg=%@",firstDimg);
    
    
    
    
    
    
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


运行程序,打印结果如下:
2013-01-04 10:42:26.691 JsonTestCao[476:c07] 4
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 200
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 成功读取
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 4
2013-01-04 10:42:26.695 JsonTestCao[476:c07] 100001
2013-01-04 10:42:26.696 JsonTestCao[476:c07] 海泰大厦
2013-01-04 10:42:26.696 JsonTestCao[476:c07] discounts==8
2013-01-04 10:42:26.697 JsonTestCao[476:c07] dimg==(
    "http://www.dianxiaor.com/dicpics/100001big/a.jpg",
    "http://www.dianxiaor.com/dicpics/100001big/b.jpg",
    "http://www.dianxiaor.com/dicpics/100001big/c.jpg",
    "http://www.dianxiaor.com/dicpics/100001big/d.jpg",
    "http://www.dianxiaor.com/dicpics/100001big/",
    "http://www.dianxiaor.com/dicpics/100001big/",
    "http://www.dianxiaor.com/dicpics/100001big/",
    "http://www.dianxiaor.com/dicpics/100001big/"
)
2013-01-04 10:42:26.697 JsonTestCao[476:c07] firstDimg=http://www.dianxiaor.com/dicpics/100001big/a.jpg
  • 大小: 18.9 KB
分享到:
评论

相关推荐

    iphone开发json解析详细讲解

    本教程将深入讲解iPhone开发中的JSON解析,帮助开发者更好地理解和运用这一技术。 首先,我们需要了解JSON的基本结构。JSON是一种轻量级的数据格式,其数据结构主要由对象(对象是一个键值对的集合,用花括号 `{}` ...

    iPhone开发-qq聊天代码

    本项目标题“iPhone开发-qq聊天代码”表明我们将深入探讨如何在iPhone应用中集成并实现QQ聊天功能,包括支持表情输入。下面将详细介绍这个过程中涉及的关键知识点。 1. **Swift或Objective-C编程**:iOS应用开发...

    iPhone开发之JSON解析示例程序

    在iPhone开发中,苹果的Foundation框架提供了一个名为`NSJSONSerialization`的类,它是处理JSON的主要工具。以下是如何使用`NSJSONSerialization`解析JSON数据的步骤: 1. **获取JSON数据**:通常,JSON数据来自于...

    Android处理JSON数据-JSON数据解析.pdf

    在Android开发中,处理JSON数据是一项基础且重要的任务。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。本篇文章主要探讨了如何在Android环境中解析...

    Json.zip_JSON_iphone_iphone json

    本资料“Json.zip”专注于讲解如何在iPhone应用中使用JSON,特别是涉及到将JSON解析为字典以及将字典转换回JSON格式的逆向操作。 首先,让我们深入理解JSON的格式。JSON是一种基于文本的格式,其数据结构主要由对象...

    ios-Json数据解析(天气预报).zip

    总的来说,这个项目涵盖了iOS开发中的网络请求、JSON解析以及模型对象的创建和使用。理解这些概念和技术对于任何iOS开发者来说都是非常基础且重要的,尤其是在构建需要与服务器交互的应用时。通过实践和学习这些知识...

    iphone开发之Json数据的解析

    iphone开发之Json数据的解析 1、将Source文件加入工程,在合适的位置添加CJSONDeserializer.h头文件。 2、从服务器请求数据、解析

    iPhone开发基础教程-PDF版

    最后,教程可能会涉及网络编程,包括使用URLSession进行HTTP请求,以及JSON解析,这些都是现代应用与服务器交互的基础。 总的来说,《iPhone开发基础教程-PDF版》提供了一个全面的学习路径,从入门到进阶,帮助...

    iPhone开发基础教程-PDF完整版 part 2

    - **JSON/XML解析**: 处理服务器返回的数据格式,实现数据与应用逻辑的交互。 - **WebSocket**: 实时双向通信技术,适用于聊天应用或实时更新场景。 #### 5. 测试与调试 - **XCTest**: 苹果提供的单元测试框架,...

    iphone-app-master.zip

    3. JSON解析:使用`JSONSerialization`或第三方库如SwiftyJSON处理JSON数据。 4. UI自动化测试:可能使用XCTest进行应用的功能测试。 5. 数据持久化:可能采用Core Data或SQLite存储用户数据。 6. 自定义控件:...

    Head First iPhone Development - Dan Pilone

    《Head First iPhone开发》这本书由Dan Pilone撰写,是一本专为希望快速掌握iPhone应用程序开发技术的编程者设计的教程。该书通过简单、步骤化的教学方式,将复杂的概念和技巧以易于理解的形式呈现给读者,使他们...

    iPhone开发基础教程&Objective-C.2.0程序设计

    8. **网络编程**:包括使用URLSession进行HTTP请求,JSON解析,以及使用Core Data存储本地数据。 9. **推送通知**:实现远程通知,让用户即使不打开应用也能获取信息。 10. **App Store发布**:包括证书配置、版本...

    iPhone3开发基础教程

    - **网络通信**:详解了如何利用NSURLSession进行网络请求,以及如何解析JSON、XML等数据格式。 #### 4. 实战案例 - **完整项目开发**:通过一个完整的项目实例,展示了从需求分析到设计、编码、测试直至发布的全...

    JSON_IPHONE

    同时,优化JSON解析和序列化性能对于提升用户体验至关重要,尤其是在处理大量数据时。 9. **JSON和Model对象映射**:在实际项目中,通常会创建特定的Model类来代表JSON数据结构,通过工具或手动方式实现JSON数据到...

    fastjson解析json.rar

    这个压缩包“fastjson解析json.rar”显然包含了使用Fastjson进行JSON解析的相关资源,包括fastjson的库文件、示例代码以及可能的数据模型类。下面将详细介绍Fastjson的使用方法以及压缩包中的关键文件。 首先,`...

    IOS应用源码之chrishamant-iphone-monopoly-37690f1.zip

    通过对源代码的详细解析,我们将揭示iOS应用开发的核心概念和技术,包括Objective-C编程语言、UIKit框架、游戏逻辑设计以及资源管理等多个方面。 首先,我们关注的是Objective-C,这是iOS应用开发的主要语言。...

    oschina-iphone-app-master(开源中国).zip

    3. **网络请求处理**:了解如何使用URLSession或其他第三方库如Alamofire进行API调用,获取和解析JSON数据。 4. **UI设计与实现**:学习如何使用Storyboard、XIB或SwiftUI创建用户界面,以及AutoLayout和Size ...

    iPhone开发指南(中-英文版)

    书中会介绍如何处理JSON数据,以及如何使用JSONSerialization进行数据解析。 在用户体验方面,iOS强调原生设计原则,遵循苹果的设计指南,提供一致且直观的交互。开发者需要学习如何利用Auto Layout和Size Classes...

    深入浅出iPhone开发

    此外,JSON是最常见的数据交换格式,书中会解释如何解析和序列化JSON数据。 iOS应用还需要处理用户的输入和交互,如触摸事件、手势识别等。此外,通知、定位服务、多媒体播放等也是iOS开发中的重要话题。本书会详细...

Global site tag (gtag.js) - Google Analytics