美文网首页
Objective-C json格式数据操作 直接编写json格

Objective-C json格式数据操作 直接编写json格

作者: 麻辣十三香小龙虾 | 来源:发表于2018-05-18 15:29 被阅读0次

开发过程中,经常需要请求服务器获取账户的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);
}

写的比较简单,可根据具体需求进行扩展。欢迎指教。

相关文章

网友评论

      本文标题:Objective-C json格式数据操作 直接编写json格

      本文链接:https://www.haomeiwen.com/subject/yqpgdftx.html