iOS 系統json資料讀取與轉換

2021-06-13 05:40:46 字數 1575 閱讀 4835

//parse out the json data

nserror* error;

nsdictionary* json = [nsjsonserialization jsonobjectwithdata:responsedata

options:kniloptions

error:&error];//1

nsarray* latestloans = [json objectforkey:@"loans"]; //2

nslog(@"loans: %@", latestloans); //3

// 1) get the latest loan

nsdictionary* loan = [latestloans objectatindex:0];

// 2) get the funded amount and loan amount

nsnumber* fundedamount = [loan objectforkey:@"funded_amount"]; nsnumber* loanamount = [loan objectforkey:@"loan_amount"];

float outstandingamount = [loanamount floatvalue] -

[fundedamount floatvalue];

humanreadble.text = [nsstring stringwithformat:@"latest loan: %@ from %@ needs another $%.2f to pursue their entrepreneurial dream", [loan objectforkey:@"name"],

[(nsdictionary*)[loan objectforkey:@"location"] objectforkey:@"country"],

outstandingamount];

//build an info object and convert to json

nsdictionary* info = [nsdictionary dictionarywithobjectsandkeys: [loan objectforkey:@"name"],

@"who",

[(nsdictionary*)[loan objectforkey:@"location"] objectforkey:@"country"],

@"where",

[nsnumber numberwithfloat: outstandingamount],

@"what",

nil];

//convert object to data

nsdata* jsondata = [nsjsonserialization datawithjsonobject:info options:nsjsonwritingprettyprinted error:&error];

//print out the data contents

jsonsummary.text = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding];

iOS 陣列 字典轉json

nsmutablearray temparr nsmutablearray array 這裡不要用for in 先找到模型,然後給可變字典賦值,最後裝到陣列裡 nsmutabledictionary dic nsmutabledictionary dictionary for int i 0 i s...

IOS學習 json轉Byte 轉化

又有乙個專案要結束了哈哈哈哈,久違的和師長一起review 這一篇是番外篇,來講一講oc中的json轉化為byte傳送的事情。這個專案要求要實現iphone與伺服器的tcp連線,然後傳送json資料。要以byte的形式傳送json資料而且前四個位元組要儲存json資料長度。那麼我們就來上 了,首先是...

Python讀取Json資料

讀取json資料,實際上是把json格式變成python中字典 列表等格式化的資料,方便索引查詢。可以新建乙個檔案命名為data,然後把下面這段json資料放進去。import json path data f open path,r encoding utf 8 m json.load f jso...