ios本地文件读取,json解析YYModel

本文介绍了如何在iOS应用中读取本地JSON文件,并使用YYModel进行解析。首先通过NSBundle获取JSON文件的绝对路径,然后演示了如何进行文件内容的读取和JSON数据的模型映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    //获取当前工程下指定文件路径,AppConfig.json文件

    NSString *filePath = [[NSBundlemainBundle]pathForResource:@"AppConfig"ofType:@"json"];


//获取绝对路径下的指定文件内容:

    NSString *path = @"/Users/gx/Desktop/test_utf8.txt";
    //NSString *str = [NSString stringWithContentsOfFile:path];
    //UTF-8编码
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",str);
    
    //GBK编码
    NSString *path2 = @"/Users/gx/Desktop/test_gbk.txt";
    NSString *str2 = [NSString stringWithContentsOfFile:path2 encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) error:nil];
    NSLog(@"%@",str2);
        
    //使用NSURL从文件中读取字符串
    NSURL *url = [NSURL URLWithString:@"file:///Users/gx/Desktop/test_utf8.txt"];
    NSString *str3 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",str3);
    
    //使用NSURL读取远程文本
    NSURL *url2 = [NSURL URLWithString:@"http://www.baidu.com"];
    NSString *str4 = [NSString stringWithContentsOfURL:url2 encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",str4);


本地json解析

json文件

{
    "PracticeID": "{c5068d68-890c-444f-8dcc-2c10ceffc721}",
    "AreaID": "RP1",
    "Points": [
        {
            "Index": 1,
            "x": 193.2760009765625,
            "y": 24.34600067138672,
            "z": 0,
            "carDir": 93.69000244140625,
            "ErrMsg": ""
        },
        {
            "Index": 2,
            "x": 193.2760009765625,
            "y": 24.34600067138672,
            "z": 0,
            "carDir": 93.69000244140625,
            "ErrMsg": ""
        },
        {
            "Index": 3,
            "x": 193.2760009765625,
            "y": 24.34600067138672,
            "z": 0,
            "carDir": 93.69000244140625,
            "ErrMsg": ""
        }
    ]
}

类1定义

#import <Foundation/Foundation.h>

@interface PointNode : NSObject
@property UInt64 index;
@property long uid;
@property float x;
@property float y;
@property float z;
@property float carDir;
@property NSString* errMsg;

-(void)initData;
-(void)showLog;
@end

#import "PointNode.h"

@implementation PointNode

-(void)initData
{
    NSLog(@"111");
//    NSString* filepath =@"mapData.json";

//    NSLog(content);
    
    
}

-(void)showLog
{
    NSLog(@"%i",_uid);
}

@end

类2定义:

#import <Foundation/Foundation.h>

@interface PathData : NSObject
@property NSString* PracticeID;
@property NSString* AreaID;
@property NSArray* Points;

@end

#import "PathData.h"

@implementation PathData

@end



YYModel解析

    NSString* testJson=@"{\"uid\":1001}";
    NSData* stringData = [testJson dataUsingEncoding:NSUTF8StringEncoding];
    id jsonid = [NSJSONSerialization JSONObjectWithData:stringData options:0 error:nil];
    PointNode* user= [PointNode yy_modelWithJSON:jsonid];
    [user showLog];
//    NSLog(@"%@",user);
    
    NSString* filepath = [[NSBundle mainBundle]pathForResource:@"paths/pathData" ofType:@"json"];
    NSString* content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
    NSData* stringData2 = [content dataUsingEncoding:NSUTF8StringEncoding];
    id jsonid2 = [NSJSONSerialization JSONObjectWithData:stringData2 options:0 error:nil];
    PathData* user2= [PathData yy_modelWithJSON:jsonid2];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值