Java 怎么将JSON对象转换为Java对象

2019年7月29日20:09:52Java 怎么将JSON对象转换为Java对象已关闭评论 364 2564字阅读8分32秒

1.简介

Java 怎么将JSON对象转换为Java对象?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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() {

    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) {
      
      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!");
  }
}

 
继续阅读
编程十万个怎么办