Skip to content

can you please help with parsing this json ? #8

@basharbachir

Description

@basharbachir

hi, may i ask you to help me please to parse this json in this url ,

http://opml.radiotime.com/Browse.ashx?render=json&c=podcast

my code is here, so what i did wrong please ?

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Children>> fetchsprodcasts(http.Client client) async {
  final response =
  await client.get("http://opml.radiotime.com/Browse.ashx?render=json&c=podcast", headers: {'Content-Type': 'application/json; charset=utf-8'});
  return compute(parseProdcasts,utf8.decode(response.bodyBytes));
}
List<Children> parseProdcasts(String responseBody) {
  final parsed = jsonDecode(responseBody)['body'].map((item)=> item['children']).toList().expand((x) => x).toList();
  return parsed.map((m) => new Children.fromJson(m)).toList();
}
class Prodcasts {
  Head head;
  List<Body> body;

  Prodcasts({this.head, this.body});

  Prodcasts.fromJson(Map<String, dynamic> json) {
    head = json['head'] != null ? new Head.fromJson(json['head']) : null;
    if (json['body'] != null) {
      body = new List<Body>();
      json['body'].forEach((v) {
        body.add(new Body.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.head != null) {
      data['head'] = this.head.toJson();
    }
    if (this.body != null) {
      data['body'] = this.body.map((v) => v.toJson()).toList();
    }
    return data;
  }
}
class Head {
  String title;
  String status;

  Head({this.title, this.status});

  Head.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['status'] = this.status;
    return data;
  }
}
class Body {
  String element;
  String text;
  List<Children> children;

  Body({this.element, this.text, this.children});

  Body.fromJson(Map<String, dynamic> json) {
    element = json['element'];
    text = json['text'];
    if (json['children'] != null) {
      children = new List<Children>();
      json['children'].forEach((v) {
        children.add(new Children.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['element'] = this.element;
    data['text'] = this.text;
    if (this.children != null) {
      data['children'] = this.children.map((v) => v.toJson()).toList();
    }
    return data;
  }
}
class Children {
  String element;
  String type;
  String text;
  String uRL;
  String guideId;

  Children({this.element, this.type, this.text, this.uRL, this.guideId});

  Children.fromJson(Map<String, dynamic> json) {
    element = json['element'];
    type = json['type'];
    text = json['text'];
    uRL = json['URL'];
    guideId = json['guide_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['element'] = this.element;
    data['type'] = this.type;
    data['text'] = this.text;
    data['URL'] = this.uRL;
    data['guide_id'] = this.guideId;
    return data;
  }
}
void main() {
  runApp(ParseProdcastss());
}
class ParseProdcastss extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  ParseProdcasts();
  }
}
class ParseProdcasts extends StatelessWidget {
  ParseProdcasts({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return  FutureBuilder<List<Children>>(
      future: fetchsprodcasts(http.Client()),
      builder: (context, snapshot) {
        if (snapshot.hasError) print(snapshot.error);
        return snapshot.hasData
            ? ProdcastsList(childrenList: snapshot.data) :Container(
          color: Colors.white,
              child: Center(child: new CircularProgressIndicator(
          backgroundColor: Color(0xff193451),
          valueColor: AlwaysStoppedAnimation<Color>(Colors.blueGrey),
        )),
            );
      },
    );
  }
}
class ProdcastsList extends StatefulWidget {
  List<Children> childrenList = []; // We assume that you filled this list with children.
  ProdcastsList({Key key, this.childrenList}) : super(key: key);
  @override
  _ProdcastsListState createState() => _ProdcastsListState();
}
class _ProdcastsListState extends State<ProdcastsList> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        body: Container(
          color: Colors.blueGrey,
          child: Column(
            children: [
              Expanded(
                child: ListView.builder(
                  physics: ClampingScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: widget.childrenList.length,
                  itemBuilder: (context, index) {
                    return ExpansionTile(
                      backgroundColor: Colors.grey.shade200,
                      children: <Widget>[
                      ],
                      title: Text('${widget.childrenList[index].text}',style: TextStyle(fontSize: 12, color: Colors.blueGrey),),
                      subtitle: Text(''),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions