java JSON 解析

使用org提供的接口解析json格式

注意:JSONObject和JSONArray不可混淆,

Json 内容,可以使用在线json格式解析如下内容:https://www.json.cn/

{"word_tag": [6, 2, 1], "word_name": "tall", "synonym": [{"part_name": "adj.", "means": [{"cis": ["excessive", "magnified", "exaggerated", "overstated", "unreasonable", "extreme"], "word_mean": "夸张的;过分的"}, {"cis": ["towering", "lofty", "high"], "word_mean": "高的"}]}, {"part_name": "", "means": [{"cis": ["lengthy", "excessive", "exaggerated", "strapping", "long", "big", "short", "extreme", "high"], "word_mean": "其他释义"}]}], "sentence": [{"ttsUrl": "http://res-tts.iciba.com/tts_new_dj/5/0/0/500036feabfd563ab4853513d17d53fb.mp3", "en": "a tall angular woman", "cn": "又高又瘦的女人"}, {"ttsUrl": "http://res-tts.iciba.com/tts_new_dj/9/3/3/933c0fbc41b5d3a7ad495d1fd2994b5a.mp3", "en": "a tall, luxuriantly bearded man", "cn": "留有浓密胡须的高个子男人"}], "exchange": {"word_noun": ["tallness"], "word_third": [], "word_done": [], "word_pl": [], "word_est": [], "word_adv": [], "word_verb": [], "word_adj": [], "word_conn": [], "word_prep": [], "word_ing": [], "word_er": [], "word_past": []}, "symbols": {"ph_en_mp3": "http://res.iciba.com/resource/amp3/oxford/0/58/73/587363708c5538732338b232b12dc91a.mp3", "ph_en": "/tɔ:l/", "ph_am": "/tɔl/", "ph_am_mp3": "http://res.iciba.com/resource/amp3/1/0/6c/e1/6ce1a133fa63d64306c463bdd3122598.mp3"}, "parts": {"mean": ["adj.身材高的,高大的,(数量)大的,(俚)过分的,夸张的"], "simpleMeans": ["身材高的,高大的,(数量)大的,(俚)过分的,夸张的"]}, "antonym": [{"part_name": "adj.", "means": [{"cis": ["short"], "word_mean": "身材高的;高大的"}]}]}

具体代码:

import org.json.simple.parser.ParseException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JSONparse {
  public static void main(String[] args)  {

    String result="{\"word_tag\": [6, 2, 1], \"word_name\": \"tall\", \"synonym\": [{\"part_name\": \"adj.\", \"means\": [{\"cis\": [\"excessive\", \"magnified\", \"exaggerated\", \"overstated\", \"unreasonable\", \"extreme\"], \"word_mean\": \"夸张的;过分的\"}, {\"cis\": [\"towering\", \"lofty\", \"high\"], \"word_mean\": \"高的\"}]}, {\"part_name\": \"\", \"means\": [{\"cis\": [\"lengthy\", \"excessive\", \"exaggerated\", \"strapping\", \"long\", \"big\", \"short\", \"extreme\", \"high\"], \"word_mean\": \"其他释义\"}]}], \"sentence\": [{\"ttsUrl\": \"http://res-tts.iciba.com/tts_new_dj/5/0/0/500036feabfd563ab4853513d17d53fb.mp3\", \"en\": \"a tall angular woman\", \"cn\": \"又高又瘦的女人\"}, {\"ttsUrl\": \"http://res-tts.iciba.com/tts_new_dj/9/3/3/933c0fbc41b5d3a7ad495d1fd2994b5a.mp3\", \"en\": \"a tall, luxuriantly bearded man\", \"cn\": \"留有浓密胡须的高个子男人\"}], \"exchange\": {\"word_noun\": [\"tallness\"], \"word_third\": [], \"word_done\": [], \"word_pl\": [], \"word_est\": [], \"word_adv\": [], \"word_verb\": [], \"word_adj\": [], \"word_conn\": [], \"word_prep\": [], \"word_ing\": [], \"word_er\": [], \"word_past\": []}, \"symbols\": {\"ph_en_mp3\": \"http://res.iciba.com/resource/amp3/oxford/0/58/73/587363708c5538732338b232b12dc91a.mp3\", \"ph_en\": \"/tɔ:l/\", \"ph_am\": \"/tɔl/\", \"ph_am_mp3\": \"http://res.iciba.com/resource/amp3/1/0/6c/e1/6ce1a133fa63d64306c463bdd3122598.mp3\"}, \"parts\": {\"mean\": [\"adj.身材高的,高大的,(数量)大的,(俚)过分的,夸张的\"], \"simpleMeans\": [\"身材高的,高大的,(数量)大的,(俚)过分的,夸张的\"]}, \"antonym\": [{\"part_name\": \"adj.\", \"means\": [{\"cis\": [\"short\"], \"word_mean\": \"身材高的;高大的\"}]}]}";
    System.out.println(result);
    try {
      JSONObject jsonTest = (JSONObject) (new JSONParser().parse(result));
      System.out.println(jsonTest.toString());//转化为字符串
      String wordName = (String) jsonTest.get("word_name");//得到word_name对应的value

      JSONArray antonymArray = (JSONArray) jsonTest.get("antonym");
      System.out.println(antonymArray.toString());
      JSONObject antonymObject=(JSONObject)antonymArray.get(0);
      JSONArray meansArray= (JSONArray) antonymObject.get("means");
      
      //synonymArray是JSONArray的对象,不可用JSONObject
      //JSONObject synonymArray=(JSONObject)jsonTest.get("synonym");
      System.out.println("*****" + "end" + "*****");
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

}

 

posted @ 2020-08-11 16:38  suwenyuan  阅读(287)  评论(0编辑  收藏  举报