在網上看了很多方法,整合了一下比較適合現在使用的。剛剛開始弄flutter,很多東西都不懂,以前也沒有做過移動開發,很是懵逼
pubspec.yaml中添加需要的包
#http
http: ^0.12.0+1
//dio 我在后面沒有使用到,但是很多例子有用這個東西,我老是在使用的時候報奇奇怪怪的錯,真的愁人
# Dart Http請求庫
dio: ^2.1.0
flutter packages get 一下
使用了json生成工具,JSONFormat4Flutter 在這里可以下載到。使用方法,在github上面也可以看到詳細內容。
將json復制到框里,點擊格式化,在右邊的標紅部分填寫自定義名稱(查看頂層類是否有名字),點擊 生成Bean 按鈕,在左邊會生成對應的解析json的類,復制到自己的代碼中即可。
如:
import 'dart:convert' show json; import 'package:http/http.dart' as http; class personData_list { List<personData> list; personData_list.fromParams({this.list}); factory personData_list(jsonStr) => jsonStr == null ? null : jsonStr is String ? new personData_list.fromJson(json.decode(jsonStr)) : new personData_list.fromJson(jsonStr); personData_list.fromJson(jsonRes) { list = jsonRes == null ? null : []; for (var listItem in list == null ? [] : jsonRes['list']) { list.add(listItem == null ? null : new personData.fromJson(listItem)); } } @override String toString() { return '{"json_list": $list}'; } } //個人帖子以及其他詳細信息 class personData { int comment_count; int curation_rewards; int pending_claimed_accounts; int post_bandwidth; int post_count; int posting_rewards; int savings_withdraw_requests; int voting_power; int witnesses_voted_for; bool can_vote; String balance; //steem余額 String created; //創建賬號時間 String delegated_vesting_shares; //代理出去的sp String json_metadata; String last_account_update; String last_owner_update; String last_post; String last_root_post; String last_vote_time; String name; //steem名 String received_vesting_shares; String recovery_account; String reward_sbd_balance; //獎勵的sbd String reward_steem_balance; //獎勵的STeem String reward_vesting_balance; //獎勵的sp String reward_vesting_steem; String savings_balance; String savings_sbd_balance; String savings_sbd_seconds; String sbd_balance; //sbd總值 String vesting_balance; String vesting_shares; //sp總值 String vesting_withdraw_rate; votingPower voting_manabar; personData.fromParams( {this.comment_count, this.curation_rewards, this.pending_claimed_accounts, this.post_bandwidth, this.post_count, this.posting_rewards, this.savings_withdraw_requests, this.voting_power, this.witnesses_voted_for, this.can_vote, this.balance, this.created, this.delegated_vesting_shares, this.json_metadata, this.last_account_update, this.last_owner_update, this.last_post, this.last_root_post, this.last_vote_time, this.name, this.received_vesting_shares, this.recovery_account, this.reward_sbd_balance, this.reward_steem_balance, this.reward_vesting_balance, this.reward_vesting_steem, this.savings_balance, this.savings_sbd_balance, this.savings_sbd_seconds, this.sbd_balance, this.vesting_balance, this.vesting_shares, this.vesting_withdraw_rate, this.voting_manabar}); personData.fromJson(jsonRes) { comment_count = jsonRes['comment_count']; curation_rewards = jsonRes['curation_rewards']; pending_claimed_accounts = jsonRes['pending_claimed_accounts']; post_bandwidth = jsonRes['post_bandwidth']; post_count = jsonRes['post_count']; posting_rewards = jsonRes['posting_rewards']; savings_withdraw_requests = jsonRes['savings_withdraw_requests']; voting_power = jsonRes['voting_power']; witnesses_voted_for = jsonRes['witnesses_voted_for']; can_vote = jsonRes['can_vote']; balance = jsonRes['balance']; created = jsonRes['created']; delegated_vesting_shares = jsonRes['delegated_vesting_shares']; json_metadata = jsonRes['json_metadata']; last_account_update = jsonRes['last_account_update']; last_owner_update = jsonRes['last_owner_update']; last_post = jsonRes['last_post']; last_root_post = jsonRes['last_root_post']; last_vote_time = jsonRes['last_vote_time']; name = jsonRes['name']; received_vesting_shares = jsonRes['received_vesting_shares']; recovery_account = jsonRes['recovery_account']; reward_sbd_balance = jsonRes['reward_sbd_balance']; reward_steem_balance = jsonRes['reward_steem_balance']; reward_vesting_balance = jsonRes['reward_vesting_balance']; reward_vesting_steem = jsonRes['reward_vesting_steem']; savings_balance = jsonRes['savings_balance']; savings_sbd_balance = jsonRes['savings_sbd_balance']; savings_sbd_seconds = jsonRes['savings_sbd_seconds']; sbd_balance = jsonRes['sbd_balance']; vesting_balance = jsonRes['vesting_balance']; vesting_shares = jsonRes['vesting_shares']; vesting_withdraw_rate = jsonRes['vesting_withdraw_rate']; voting_manabar = jsonRes['voting_manabar'] == null ? null : new votingPower.fromJson(jsonRes['voting_manabar']); } @override String toString() { return '{"comment_count": $comment_count,"curation_rewards": $curation_rewards,"pending_claimed_accounts": $pending_claimed_accounts,"post_bandwidth": $post_bandwidth,"post_count": $post_count,"posting_rewards": $posting_rewards,"savings_withdraw_requests": $savings_withdraw_requests,"voting_power": $voting_power,"witnesses_voted_for": $witnesses_voted_for,"can_vote": $can_vote,"balance": ${balance != null ? '${json.encode(balance)}' : 'null'},"created": ${created != null ? '${json.encode(created)}' : 'null'},"delegated_vesting_shares": ${delegated_vesting_shares != null ? '${json.encode(delegated_vesting_shares)}' : 'null'},"json_metadata": ${json_metadata != null ? '${json.encode(json_metadata)}' : 'null'},"last_account_update": ${last_account_update != null ? '${json.encode(last_account_update)}' : 'null'},"last_owner_update": ${last_owner_update != null ? '${json.encode(last_owner_update)}' : 'null'},"last_post": ${last_post != null ? '${json.encode(last_post)}' : 'null'},"last_root_post": ${last_root_post != null ? '${json.encode(last_root_post)}' : 'null'},"last_vote_time": ${last_vote_time != null ? '${json.encode(last_vote_time)}' : 'null'},"name": ${name != null ? '${json.encode(name)}' : 'null'},"received_vesting_shares": ${received_vesting_shares != null ? '${json.encode(received_vesting_shares)}' : 'null'},"recovery_account": ${recovery_account != null ? '${json.encode(recovery_account)}' : 'null'},"reward_sbd_balance": ${reward_sbd_balance != null ? '${json.encode(reward_sbd_balance)}' : 'null'},"reward_steem_balance": ${reward_steem_balance != null ? '${json.encode(reward_steem_balance)}' : 'null'},"reward_vesting_balance": ${reward_vesting_balance != null ? '${json.encode(reward_vesting_balance)}' : 'null'},"reward_vesting_steem": ${reward_vesting_steem != null ? '${json.encode(reward_vesting_steem)}' : 'null'},"savings_balance": ${savings_balance != null ? '${json.encode(savings_balance)}' : 'null'},"savings_sbd_balance": ${savings_sbd_balance != null ? '${json.encode(savings_sbd_balance)}' : 'null'},"savings_sbd_seconds": ${savings_sbd_seconds != null ? '${json.encode(savings_sbd_seconds)}' : 'null'},"sbd_balance": ${sbd_balance != null ? '${json.encode(sbd_balance)}' : 'null'},"vesting_balance": ${vesting_balance != null ? '${json.encode(vesting_balance)}' : 'null'},"vesting_shares": ${vesting_shares != null ? '${json.encode(vesting_shares)}' : 'null'},"vesting_withdraw_rate": ${vesting_withdraw_rate != null ? '${json.encode(vesting_withdraw_rate)}' : 'null'},"voting_manabar": $voting_manabar}'; } } class votingPower { int last_update_time; String current_mana; votingPower.fromParams({this.last_update_time, this.current_mana}); votingPower.fromJson(jsonRes) { last_update_time = jsonRes['last_update_time']; current_mana = jsonRes['current_mana']; } @override String toString() { return '{"last_update_time": $last_update_time,"current_mana": ${current_mana != null ? '${json.encode(current_mana)}' : 'null'}}'; } }
那么接下來就是使用,在對應的界面中,添加一下代碼://貼出了主要的代碼,后面的使用 就看具體情況啦
在這之前,有大佬告誡過我,flutter 放棄吧,json解析很煩的,還有各種坑等着你,我也沒辦法,我不是老大說了不算啊,硬着頭皮啃吧。
import 'package:flutter/material.dart';
import 'package:項目名/data/data_index.dart';//這個就是上面那個解析json文件的位置
import 'package:http/http.dart' as http;
class PersonPager extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new PersonPagerState();
}
}
class PersonPagerState extends State<PersonPager> {
proFile _profile;
void get() async { var userList = await fetchAndParseUsers(); var projson; userList.forEach( (user) {
//這里就是將自己需要的字段取出來 personName = user.name; projson = user.json_metadata;//這個東西還需要解析 }, ); _profile = await ParseProfile(projson);//這個就是解析json_metadata的數據 } //獲取個人信息 Future<List<personData>> fetchAndParseUsers() async { apiUrl = SteemApi.steemApiUrl + SteemApi.getPerson;//這個是請求的URL var res = await http.get(apiUrl + '?names[]=["$name"]');//這個版本的http 我很難理解 為什么get沒有參數這個東西了,post請求是有的,所以我就很懶的 這樣拼上去了 var jsonStr = res.body;//在這之前 用的是Response 發現沒有body這個東西,使用它的data 又是各種報錯,捶地!!!!!所以我就使用了http // print(res.body); var parsedUserList = json.decode(jsonStr); var userList = <personData>[]; parsedUserList.forEach((parsedUser) { userList.add(new personData.fromJson(parsedUser)); }); return userList; }
//后面的json_metadata解析使用的方法 放在這后面
}
json_metadata 這個參數,又需要解析,我試着把這個跟上面的數據一起解析,發現不行,因為這里多了一層?不是很清楚,有大佬知道,希望可以解答一下。感覺應該是可以一起解析的,我這里分開解析了
還是用json快速解析工具 得到以下內容:當然啦 這個不是很重要
import 'dart:convert' show json; class proFileList { proFile profile; proFileList.fromParams({this.profile}); factory proFileList(jsonStr) => jsonStr == null ? null : jsonStr is String ? new proFileList.fromJson(json.decode(jsonStr)) : new proFileList.fromJson(jsonStr); proFileList.fromJson(jsonRes) { profile = jsonRes['profile'] == null ? null : new proFile.fromJson(jsonRes['profile']); } @override String toString() { return '{"profile": $profile}'; } } class proFile { String about; String cover_image; String location; String name; String profile_image; String website; proFile.fromParams( {this.about, this.cover_image, this.location, this.name, this.profile_image, this.website}); proFile.fromJson(jsonRes) { about = jsonRes['about']; cover_image = jsonRes['cover_image']; location = jsonRes['location']; name = jsonRes['name']; profile_image = jsonRes['profile_image']; website = jsonRes['website']; } @override String toString() { return '{"about": ${about != null ? '${json.encode(about)}' : 'null'},"cover_image": ${cover_image != null ? '${json.encode(cover_image)}' : 'null'},"location": ${location != null ? '${json.encode(location)}' : 'null'},"name": ${name != null ? '${json.encode(name)}' : 'null'},"profile_image": ${profile_image != null ? '${json.encode(profile_image)}' : 'null'},"website": ${website != null ? '${json.encode(website)}' : 'null'}}'; } }
這個東東就很煩了,上面的那種使用方法就行不通了。
//解析json_metadata Future<proFile> ParseProfile(var jsonStr) async { final parsedUserList = json.decode(jsonStr); var list = parsedUserList["profile"];//這里有兩層,一開始 沒帶參數,后面就會是個空值 proFile pro = new proFile.fromJson(list); return pro; }
調試的時候發現,是兩層
遇到bug,錯誤 還是不要害怕,能調試就慢慢來解決,反正我頭鐵
希望上面的東西能幫到你=。=