Simple JSONは、Apacheプロトコルに基づいてGoogleが開発したJava JSON解析フレームワークです。
json-simpleのホームページ://www.vevb.com/softs/455885.html
ダウンロードされたファイルはjson_simple.jarです
例1: JSonValueを使用する非常に便利な方法
System.out.println( "=============================================== obj = jsonArray.out.println(jsonarray); obj2 =(jsonobject)array.get(1); S = "{}"JSonObjectはMAPを継承し、JsonArrayはリストを継承しているため、標準のMAPとリストを使用してJSonObjectとJSonArrayを使用できます。
JSonValueは、配列またはオブジェクトを使用できます。
例2:JsonParserを使用したクイックウェイ
jsonparser parser = new JSonParser(); System.out.println( "======================================================================================オブジェクトOBJ = PARSER array =(jsonarray)obj.out.println( "================== System.out.println( "======================================== s = "5、] parser.out.println(obj);
JSonParserを使用するには、例外をキャッチする必要があります。
例3:例外処理
string jsontext = "[[null、123.45、/" a // tb c/"]}、true"; jsonparser parser = new JSonParser(); try {parser.parse(jsontext); } catch(parseexception pe){system.out.println( "position:" + pe.getposition()); System.out.println(PE); }実行結果:
位置:25位置の25の予期しないトークン右ブレース(})25。
例4:コンテナ工場
ContainerFactoryクラスを使用して、コンテナ工場を作成します。
string jsontext = "{/" first/":123、/" second/":[4、5、6]、/" third/":789}"; jsonparser parser = new JSonParser(); containerfactory containerfactory = new containerfactory(){public listCreatArrayContainer(){return new LinkedList(); } public Map CreateObjectContainer(){return new LinkedHashmap(); }}; try {map json =(map)parser.parse(jsontext、containerfactory); iterator iter = json.entryset()。iterator(); system.out.println( "== iterate result =="); while(iter.hasnext()){map.entry entry =(map.entry)iter.next(); System.out.println(entry.getKey() + "=>" + entry.getValue()); } system.out.println( "== tojsonstring()=="); System.out.println(jsonvalue.tojsonstring(json)); } catch(parseexception pe){system.out.println(pe); }結果は次のとおりです。
== iterate result == first => 123 second => [4,5,6] 3番目=> 789 == tojsonstring()== {"First":123、 "second":[4,5,6]、 "third":789}コンテナ工場を使用していない場合、Simple-JSONはデフォルトでJSonObjectとJSonArrayを使用します。例5:停止可能なSAXコンテンツ処理SimpleJSONは、テキストストリームを処理するための簡単な停止可能なSAXコンテンツ処理方法を推奨します。ユーザーは、論理入力ストリームの任意の時点にとどまり、他のロジックを処理し、以前の処理を続行できます。ストリーム全体が処理されるのを待たないでください。これが例です。 keyfinder.java:クラスKeyFinderはContentHandlerを実装しています{プライベートオブジェクト値。 private boolean fund = false;プライベートブールエンド= false;プライベート文字列キー。プライベートストリングマッチキー。 public void setMatchkey(String MatchKey){this.matchkey = matchkey; } public Object getValue(){return値; } public boolean isend(){return end; } public void setFound(boolean fund){this.found = fund; } public boolean isfound(){return fund; } public void startjson()throws parseexception、ioexception {fund = false; end = false; } public void endjson()throws parseexception、ioexception {end = true; } public boolean primitive(object value)throws parseexception、ioexception {if(key!= null){if(key.equals(matchkey)){fund = true; this.value = value; key = null; falseを返します。 }} trueを返します。 } public boolean startarray()throws parseexception、ioexception {return true; } public boolean startobject()throws parseexception、ioexception {return true; } public boolean startObjectEntry(String key)Throws parseexception、ioexception {this.key = key; trueを返します。 } public boolean endarray()throws parseexception、ioexception {return false; } public boolean endobject()throws parseexception、ioexception {return true; } public boolean endobjectentry()はparseexception、ioexception {return true; }}主なロジック: string jsontext = "{/" first/":123、/" second/":[{/" k1/":{/" id/":/" id1/"}}、4、5、6、{/" id/":123}] jsonparser parser = newJsonParser(); KeyFinder Finder = newKeyFinder(); Finder.setMatchKey( "ID"); try {while(!finder.isend()){parser.parse(jsontext、finder、true); if(finder.isfound()){finder.setFound(false); system.out.println( "fund id:"); system.out.println(finder.getValue()); }}} catch(parseexception pe){pe.printstacktrace(); }実行結果:IDの見つかった:ID1 FOUND ID:123 FOUND ID:NULL例6:オブジェクトグラフ全体がSAXのようなものを使用して分析されます
クラストランスはContentHandlerを実装しています{プライベートスタックValuestack; public Object getResult(){if(valuestack == null || valuestack.size()== 0)return null; returnvaleStack.peek(); } public boolean endarray()throws parseexception、ioexception {trackback(); trueを返します。 } public void endjson()throws parseexception、ioexception {} public boolean endobject()throws parseexception、ioexception {trackback(); trueを返します。 } public boolean endobjectentry()throws parseexception、ioexception {object value = valuestack.pop(); Object key = valuestack.pop();マップ親=(マップ)Valuestack.peek(); parent.put(key、value); trueを返します。 } private void tcrackback(){if(valuestack.size()> 1){object value = valuestack.pop();オブジェクトprev = valuestack.peek(); if(prev instanceof string){valuestack.push(value); }}} private void consumptionValue(object value){if(valuestack.size()== 0)valuestack.push(value); else {object prev = valuestack.peek(); if(prev instanceof list){list array =(list)prev; array.add(value); } else {valuestack.push(value); }}} public boolean primitive(object value)throws parseexception、ioexception {consumptionValue(value); trueを返します。 } public boolean startarray()throws parseexception、ioexception {list array = new jsonarray(); ConsumptionValue(配列); valuestack.push(array); trueを返します。 } public void startjson()throws parseexception、ioexception {valuestack = new stack(); } public boolean startobject()throws parseexception、ioexception {map object = new jsonobject(); Consumevalue(object); valuestack.push(object); trueを返します。 } public boolean startObjectEntry(String key)Throws parseexception、ioexception {valuestack.push(key); trueを返します。 }}主なメソッドロジック:文字列jsonstring = <input json text>;オブジェクト値= null; jsonparser parser = new JSonParser();トランストランス= new Transformer(); parser.parse(jsonstring、transformer); value = transformer.getResult();実行結果:
文字列jsonstring = <input json text>;オブジェクト値= null; jsonparser parser = newJsonParser(); value = parser.parse(jsonstring);注:jsonpauserはスレッドセーフではありません。
JSON_ENCODE -JSON変数のエンコード。
説明:string json_encode ($ value)は、値のJSON形式を返します。
パラメーター:エンコードされる値は、リソースタイプを除く任意のデータ型にすることができます
この関数はUTF-8エンコードされたデータのみを受け入れることができます(翻訳注:文字/文字列タイプのデータを参照)
返品値:エンコーディングが成功した場合、JSONで表される文字列が返されます。