Pacific-Design.com

    
Home Index

1. Java

2. JSON

+ VideoChannel

Java / JSON /

package com.kevinduraj;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class App {

    public static void main(String[] args) throws JSONException {

        createJSON();
        //videoChannel();
        //top10Videos();
    }

    private static void createJSON() throws JsonIOException {
        ArrayList<VideoChannel> list = new ArrayList<>();
        list.add(new VideoChannel("UC5QHW3FOSmvkj4l39x5LeIw",
                "2016-12-03", 3, 12, 5, 23, 12, 11, 0, 1));

        list.add(new VideoChannel("UC5QHW3FOSmvkj4l39x5LeIw",
                "2016-12-02", 7, 17, 11, 12, 3, 31, 21, 1));

        list.add(new VideoChannel("UC5QHW3FOSmvkj4l39x5LeIw",
                "2016-11-30", 8, 1, 0, 3, 0, 4, 7, 1));

        //Flat JSON - Gson gson = new Gson();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        String out1 = gson.toJson(list);
        System.out.println(out1);

//        JsonElement je = gson.toJsonTree(list);
//        String prettyJsonString = gson.toJson(je);
//        System.out.println(prettyJsonString);

        try {
            FileWriter fw = new FileWriter("UC5QHW3FOSmvkj4l39x5LeIw.json");
            gson.toJson(list, fw);
            fw.close();
        } catch (IOException ex) {

        }

    }

    private static void videoChannel() throws JSONException {
        ArrayList<JSONObject> list = new ArrayList<>();

        JSONObject obj1 = new JSONObject();
        obj1.put("channel_id", "UC5QHW3FOSmvkj4l39x5LeIw");
        obj1.put("date", "2016-11-18");
        obj1.put("views", 3.0);
        obj1.put("likes", 0.0);
        obj1.put("dislikes", 0.0);
        obj1.put("comments", 0.0);
        obj1.put("avg-view", 0.0);
        obj1.put("mins-watch", 0.0);
        obj1.put("subs-gained", 0.0);
        obj1.put("subs-lost", 0.0);

        JSONObject obj2 = new JSONObject();
        obj2.put("channel_id", "UC5QHW3FOSmvkj4l39x5LeIw");
        obj2.put("date", "2016-11-30");
        obj2.put("views", 8.0);
        obj2.put("likes", 4.0);
        obj2.put("dislikes", 0.0);
        obj2.put("comments", 1.0);
        obj2.put("avg-view", 55.0);
        obj2.put("mins-watch", 0.0);
        obj2.put("subs-gained", 0.0);
        obj2.put("subs-lost", 0.0);

        JSONObject obj3 = new JSONObject();
        obj3.put("channel_id", "UC5QHW3FOSmvkj4l39x5LeIw");
        obj3.put("date", "2016-12-02");
        obj3.put("views", 1.0);
        obj3.put("likes", 0.0);
        obj3.put("dislikes", 0.0);
        obj3.put("comments", 0.0);
        obj3.put("avg-view", 0.0);
        obj3.put("mins-watch", 7.0);
        obj3.put("subs-gained", 0.0);
        obj3.put("subs-lost", 0.0);

        JSONObject obj4 = new JSONObject();
        obj4.put("channel_id", "UC5QHW3FOSmvkj4l39x5LeIw");
        obj4.put("date", "2016-12-03");
        obj4.put("views", 2.0);
        obj4.put("likes", 0.0);
        obj4.put("dislikes", 0.0);
        obj4.put("comments", 0.0);
        obj4.put("avg-view", 0.0);
        obj4.put("mins-watch", 0.0);
        obj4.put("subs-gained", 0.0);
        obj4.put("subs-lost", 0.0);

        String out = new JSONObject(obj1.toString()).toString(2);
        System.out.print("\n" + out + "\n");

    }

    private static void top10Videos() throws JSONException, JsonSyntaxException {
        
        JSONObject obj = new JSONObject();

        obj.put("video_id", "gxBibG-6bjQ");
        obj.put("views", 31.0);
        obj.put("likes", 5.0);
        obj.put("dislikes", 0.0);
        obj.put("comments", 2.0);
        obj.put("avg-view", 2.0);
        obj.put("mins-watch", 2.0);
        obj.put("subs-gained", 1.0);
        obj.put("subs-lost", 0.0);

        //System.out.print(obj1 + "\n");
        System.out.println(Strings.repeat("-", 120));

        prettyJSON(obj);
    }

    private static void prettyJSON(JSONObject obj) throws JsonSyntaxException {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();

        JsonElement je = jp.parse(obj.toString());
        String prettyJsonString = gson.toJson(je);
        System.out.println(prettyJsonString);
    }

}