如何在Flutter中更新本地JSON字段

时间:2019-06-28 04:37:01

标签: json flutter dart

我是扑朔迷离的飞镖新手,我阅读了很多Web文章和文档,但无法弄清楚如何更新JSON字段,我有一个本地JSON文件,例如

 {
"category": "Happiness",
"quotes":[
  {
  "quote":"I hope you will find a reason to smile",
  "favorite":false
  },
  {
  "quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
  "favorite":false
  }]}

我想将JSON文件中的归档收藏夹更新为true,有什么办法

5 个答案:

答案 0 :(得分:0)

您可以在JSON中的quotes列表上使用forEach循环,并将其中的favourite设置为true。我假设您已经使用StringjsonDecodejson.decode数据解析为JSON。

如果没有,这是有关如何在Dart中解析JSON数据的link

考虑以下代码-

Map<String, dynamic> jsonData =  {
    "category": "Happiness",
    "quotes":[
     {
          "quote":"I hope you will find a reason to smile",
          "favorite":false
     },
     {
          "quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
          "favorite":false
     }
]};

(jsonData["quotes"] as List<dynamic>).forEach((item) {
    item["favorite"] = true;
});

您还可以为forEach循环函数使用简写语法,如下所示-

(jsonData["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);

答案 1 :(得分:0)

首先将此json数组带到任何变量,然后了解层次结构。

例如“ jsonArr”是保存该数据的数组,然后..

jsonArr ['quotes'] [0] ['favorite'] = true;

或者您可以将其放入for循环中,并将该循环遍历到json数组的长度。

答案 2 :(得分:0)

您可以像这样解析JSON

Map<String, dynamic> data = jsonDecode(jsonString);

如果要使每个收藏夹都为真,则可以使用像这样的循环

(data["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);

如果要设置单个对象值true,则需要像这样传递position

(data["quotes"] as List<dynamic>)[position]['favorite'] = true;

添加

Here是用于编码和解码JSON的链接

答案 3 :(得分:0)

您可以使用以下方法。按下按钮后,字段favorite更改为true

FlatButton(
    child: Text('Add Favourite'),
    onPressed: (){
         myJson['quotes'][myIndex]['favorite'] = true;
})

答案 4 :(得分:0)

如果您使用JSON模型并将它们声明为可变模型,则可以直接在源数据上执行此修改。

import 'dart:convert';

import 'json_objects.dart';

void main() {
  var json = jsonDecode(_source) as Map<String, dynamic>;
  var response = Response1.fromJson(json);
  for (var quote in response.quotes) {
    quote.favorite = false;
  }

  json = response.toJson();
  print(json);
}

final _source = r'''
{
    "category": "Happiness",
    "quotes": [
        {
            "quote": "I hope you will find a reason to smile",
            "favorite": false
        },
        {
            "quote": "Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
            "favorite": false
        }
    ]
}''';

结果:

{category: Happiness, quotes: [{favorite: false, quote: I hope you will find a reason to smile}, {favorite: false, quote: Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.}]}

使用的JSON模型。

class Response1 {
  String category;
  List<Response1Quotes> quotes;

  Response1({this.category, this.quotes});

  factory Response1.fromJson(Map<String, dynamic> json) {
    return Response1(
      category: json['category'] as String,
      quotes: _toObjectList(json['quotes'], (e) => Response1Quotes.fromJson(e)),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'category': category,
      'quotes': _fromList(quotes, (e) => e.toJson()),
    };
  }
}

class Response1Quotes {
  bool favorite;
  String quote;

  Response1Quotes({this.favorite, this.quote});

  factory Response1Quotes.fromJson(Map<String, dynamic> json) {
    return Response1Quotes(
      favorite: json['favorite'] as bool,
      quote: json['quote'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'favorite': favorite,
      'quote': quote,
    };
  }
}

List _fromList(data, Function(dynamic) toJson) {
  if (data == null) {
    return null;
  }
  var result = [];
  for (var element in data) {
    var value;
    if (element != null) {
      value = toJson(element);
    }
    result.add(value);
  }
  return result;
}

List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
  if (data == null) {
    return null;
  }
  var result = <T>[];
  for (var element in data) {
    T value;
    if (element != null) {
      value = fromJson(element as Map<String, dynamic>);
    }
    result.add(value);
  }
  return result;
}

/*
Response1:
  "category": String
  "quotes": List<Response1Quotes>

Response1Quotes:
  "quote": String
  "favorite": bool
*/