从Flutter中的复杂JSON提取数据

时间:2018-10-22 20:26:30

标签: json dart flutter

我正在从结构未知的更复杂的JSON中提取数据,其结构随操作而变化, JSON示例链接:http://afs-i.com/json.json

请在这里找到我的代码:http://afs-i.com/main.dart

预先感谢

更新: 我使用PHP代码提取了数据,您可以在此处找到结果:http://afs-i.com/json.php

请问这是我的PHP代码:

$arraycars=array();
$y=json_decode($x);
//  echo "<pre>";
//  var_dump($y->tree[0]->children); 
foreach ($y->tree[0]->children as $f) {
  if(isset($f->vid)){
       global $arraycars;
       $arraycars[]=$f;
   } elseif(isset($f->children)){
     if(sizeof($f->children) > 0){
        coolectcars($f->children);
     }
    }   
   }
   function coolectcars($array){
   // var_dump($array);
   foreach ($array as $f) {
     if(isset($f->vid)){
        global $arraycars;
        $arraycars[]=$f;
     } elseif(isset($f->children)){
        if(sizeof($f->children) > 0){
            coolectcars($f->children);
        }
     }  
   }
  }
  echo json_encode($arraycars);

更新:2 我现在有此代码的null错误问题: 错误:

I/flutter ( 4264): NoSuchMethodError: The method 'forEach' was called on 
 null.
I/flutter ( 4264): Receiver: null
I/flutter ( 4264): Tried calling: forEach(Closure: (Children) => Null)

代码:

List<Children> cars = [];
Queue numQ = new Queue();
numQ.addAll(parsed["tree"][0]["children"]);
Iterator i = numQ.iterator;

while (i.moveNext()) {
//    print("ddddddd ${i.current}");
 if (i.current.containsKey("vid")) {
  cars.add(new Children(
      i.current['vid'], i.current['protocol'], i.current['datetime'],
      i.current['gpss']));
  } else {
  Queue numQ = new Queue();
  if (i.current["children"] != null) {
    numQ.addAll(i.current["children"]);
  //        iterate(numQ);
    List<Children> carse=[];
     carse = iterate(numQ);
     carse.forEach((data){
       cars.add(data);
     }) ;

   }
  }
 }
 cars.forEach((data) {
  print(data.toString());
 });
 List<Children> iterate(Queue numQ) {
 List<Children> cars=new List<Children>();
 Iterator i = numQ.iterator;
 while (i.moveNext()) {
 print("ddddddd ${i.current}");
 if (i.current.containsKey("vid")) {
  cars.add(new Children(
      i.current['vid'], i.current['protocol'], i.current['datetime'],
      i.current['gpss']));
  } else {
  if (i.current["children"] != null) {
    Queue numQ = new Queue();
    numQ.addAll(i.current["children"]);
    List<Children> carse=[];
    carse = iterate(numQ);
    carse.forEach((data){
      cars.add(data);
    }) ;
  }
  }
 return cars;
 } 
 }

4 个答案:

答案 0 :(得分:1)

我更喜欢使用built_value进行json反序列化/序列化。更优雅。您无需自己写下fromJsonbuilt_value将为您生成解串器/串行器。您可以查看build_value的githubthisthis文章。

答案 1 :(得分:0)

插入此import 'dart:convert';  进入顶部的小部件文件。

让我们说您的json位于

var response

然后

var jsonDecoded= json.decode(response);

现在您可以像这样获得名称和user_id:

var user_id= jsonDecoded["tree"][0]["user_id"];

var name = jsonDecoded["tree"][0]["name"];

注意:我从第一个对象(0索引)获得这些值。如果需要每个对象的值,则可以循环获取它。

答案 2 :(得分:0)

将JSON转换为飞镖here

的好地方

reddit url的帽子

只需将您的json复制到文本框中并生成,它将自动为您生成。

使用此方法,您可以调用setdiff并为其提供json,然后还可以为其自动完成

例如:用法

fromJson

答案 3 :(得分:0)

我得出的最终答案:

final parsed = json.decode(response.body);
List<Children> cars = [];
Queue numQ = new Queue();
numQ.addAll(parsed["tree"][0]["children"]);
Iterator i = numQ.iterator;

while (i.moveNext()) {
  if (i.current.containsKey("vid")) {

    cars.add(new Children(
        i.current['vid'],
        i.current['datetime'],
       ));

} else {
  Queue numQ = new Queue();
  if (i.current["children"].toString() != "[]") {
    numQ.addAll(i.current["children"]);
    List<Children> carse = [];
    carse = iterate(numQ);
    carse.forEach((data) {
      cars.add(data);
    });
  }
}
}


List<Children> iterate(Queue numQ) {
List<Children> cars = new List<Children>();
Iterator i = numQ.iterator;
while (i.moveNext()) {
if (i.current.containsKey("vid")) {
  if (i.current.containsKey("vid")) {

    cars.add(new Children(
        i.current['vid'],
        i.current['datetime'],
       ));

}
 } else {
  if (i.current["children"].toString() != "[]") {
  Queue numQ = new Queue();
  if (i.current["children"].toString() != "[]") {
    numQ.addAll(i.current["children"]);
    List<Children> carse = [];
    carse = iterate(numQ);
    carse.forEach((data) {
      cars.add(data);
    });
  }
  }
}
 return cars;
 }
相关问题