Convert Json object to Java Object - Java JSON

Java examples for JSON:JSON Data

Description

Convert Json object to Java Object

Demo Code



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;

import javax.swing.JButton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

class DataObject {
  List<Map<String, String>> menuItem = new ArrayList<Map<String, String>>();

  DataObject() {//  w  w  w .  j a  v a2 s  .com

    Map<String, String> map01 = new HashMap<>();
    map01.put("value", "New");
    map01.put("onclick", "CreateNewDoc()");
    menuItem.add(map01);

    Map<String, String> map02 = new HashMap<>();
    map02.put("value", "Open");
    map02.put("onclick", "OpenDoc()");
    menuItem.add(map02);

    Map<String, String> map03 = new HashMap<>();
    map03.put("value", "Close");
    map03.put("onclick", "CloseDoc()");
    menuItem.add(map03);

  }

  @Override
  public String toString() {
    JSONStringer ja = new JSONStringer();
    try {
      ja.object();
      ja.key("menu").object();

      ja.key("id").value("file").key("value").value("File");
      ja.key("popup").object();
      ja.key("menuitem").array();

      for (Map<String, String> map : menuItem) {
        ja.object();
        Iterator<Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
          Map.Entry<String, String> entry = it.next();
          ja.key(entry.getKey()).value(entry.getValue());
        }
        ja.endObject();
      }
      ja.endArray();

      ja.endObject();
      ja.endObject();
      ja.endObject();

    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return ja.toString();
  }

}

public class Main {

  public static void main(String[] args) throws Exception{
    DataObject doo = new DataObject();
    String source = doo.toString();
    System.out.println(source);
    JSONObject jobject = new JSONObject(source);
    JSONObject menuObject = jobject.getJSONObject("menu");

    String attrId = menuObject.getString("id");
    String attrValue = menuObject.getString("value");

    JSONObject popupObject = menuObject.getJSONObject("popup");
    JSONArray menuItemArray = popupObject.getJSONArray("menuitem");

    for (int i = 0; i < menuItemArray.length(); i++) {
      System.out.println(menuItemArray.getJSONObject(i).getString("value").toString());
    }

    String first = "";
    String second = "";
    Integer.compare(first.length(), second.length());

    Comparator<String> cmp = (f, s) -> {
      return Integer.compare(f.length(), s.length());
    };

    String[] words = new String[] {};
    Arrays.sort(words, (f, s) -> Integer.compare(f.length(), s.length()));

    BiFunction<String, String, Integer> t = (f, s) -> Integer.compare(f.length(), s.length());

    Arrays.sort(words, String::compareTo);
  }
}

class ConcurrentGreeter extends Greeter {
  public void greet() {
    Thread t = new Thread(super::greet);
    t.start();
  }
}

class Greeter {
  public void greet() {
    System.out.println("Hello World!");
  }
}

Related Tutorials