开发过程中,经常需要请求服务器获取账户的json数据,影响开发效率,可以使用下列方法,请求一次,之后把json数据直接写在.m中,即可直接读取。[oc语法字符串前缀为@符号,此处宏做了兼容处理]
完整Demo的GitHub地址:(https://github.com/songzhibing/SHelpTestJsonUtil/tree/master/SHelpTestJsonUtil)
//
// SHelpTestJsonUtil.h
// SHelpTestJsonUtil
//
// Created by songzhibing on 2018/5/18.
// Copyright © 2018年 com.songzhibing.help.cn. All rights reserved.
//
#import <Foundation/Foundation.h>
#define __shelp__convert__json__to_sting(...) #__VA_ARGS__
NSString *Test_Account_Data_String = @__shelp__convert__json__to_sting(
{
"List": [
{
"AcctShare": "10184.31",
"DyAmount": "4.263%",
"ProdSubId": "000343",
"SignState": "0",
"AcctNo": "8888888",
"ProdId": "0001",
"FundAcNo": "888888888",
"ProdType": "02",
"RybType": "1",
"AliasName": "app",
"TotalAmount": "10184.31",
"OpenDate": "2018-05-16",
"TotalProfit": "3207.91",
"ProfitPerAcc": "1.100500",
"FundCifNo": "8888888",
"LastProfit": "1.12",
"acLevel": "2",
"FundAmount": "10184.31",
"ProdSubName": "基金",
"EcifAmount": "0.00"
}
],
"AcStateName": "正常",
"CifNo": "",
"MobilePhone": "88888888",
"AvailBalance": "10184.31",
"OnwayAll": "0.00",
"productList": [
{
"accountDetailProductName": "如意宝余额",
"productId": "02",
"groupText": "",
"productName": "如意宝(元)",
"productSecondLineTipIcon": null,
"productFirstLineValue": "10,184.31",
"productSecondLineValue": "昨日收益: 1.12",
"accountDetailProductValue": "10184.31",
"accountDetailProductColor": "964525",
"groupId": "10001"
},
{
"accountDetailProductName": "民生金市值",
"productId": "04",
"groupText": "",
"productName": "民生金(克)",
"productSecondLineTipIcon": null,
"productFirstLineValue": "0.00",
"productSecondLineValue": "参考市值(元): 0.00",
"accountDetailProductValue": "0.00",
"accountDetailProductColor": "eaa90c",
"groupId": "10001"
}
],
"CifName": "小龙虾",
"DirectFundAmount": 0,
"Sex": "1"});
#import "SHelpTestJsonUtil.h"
- (void)test_json
{
//测试代码:
NSString *testAcStr = Test_Account_Data_String;
NSData *json_data = [testAcStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json_dict = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",json_dict);
}
写的比较简单,可根据具体需求进行扩展。欢迎指教。
网友评论