json解析 本地檔案和網上檔案 通過介面

2021-07-04 15:54:33 字數 1476 閱讀 8371

nsstring *path =[[nsbundle mainbundle] pathforresource:@「movielist」 oftype:@「txt」];
// 第二個引數,制定乙個容器來接收解析後的資料
nsmutabledictionary *dic =[nsjsonserialization jsonobjectwithdata:data option:nsjsonreadingmutablecontainers error:nil];

nsmutablearray *moviearr =[nsmutablearray array];

for (nsmutabledictionary *temp in dic[@"result"])

對網上檔案的解析

原**

nsstring *strurl =@"銀行®ion=大連&output=json&ak=6e823f587c95f0148c19993539b99295";
乙個正常的url位址是不允許有中文的,只能有數字, 26個英文本母的大小寫,和一些特殊的符號,比如 &,%等,如果遇到帶中文的url,首先不它進行編碼

原**:

nsstring *strencode=[strurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
接下來,url符合要求之後,就開始進行網路請求,網路請求分為三步

1.根據已經編好的url,建立乙個nsurl

nsurl *url =[nsurl urlwithstring:strencode];
2.傳送乙個請求

原**:

nsmutableurlrequest *request =[nsmutableurlrequest requestwithurl:url];
3.返回我們要的資料,乙個nsdata物件

三個引數:第乙個引數是剛剛建立的請求,第二個是返回的乙個響應,第三個是錯誤資訊

原**

nsurlresponse *response = nil;

nserror *error =nil;

nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response

error:&error];

把所有的銀行名都列印出來

原**:

nsmutabledictionary *dic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:nil];

for (nsdictionary *dic1 in dic[@"results"])

讀取本地json檔案,解析json

data.json 檔案同目錄下 import json 引入模組 count 1 開啟乙個json檔案 data open data.json encoding utf 8 轉換為python物件 strjson json.load data flag false lockflag false w...

讀取本地json檔案,解析json 例項講解

模擬使用者登入 data.json 檔案同目錄下 import json 引入模組 count 1 開啟乙個json檔案 data open data.程式設計客棧json encoding utf 8 轉換為python物件 strjson json.load data flag false lo...

JSON庫解析json檔案

cocoa 下json開源的類庫有很多,其中jsonkit庫是非常簡單易用而且效率又比較高的。解析 舉例 import jsonkit.h 假設 strjson 是網路上接收到的 json 字串,nsstring strjson bage 3,sound def.nsdictionary resul...