JSONObjectJSONWriter和JSONTokener类的一些用法组织.json
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSONObjectJSONWriter和JSONTokener类的一些用法组织.json相关的知识,希望对你有一定的参考价值。
This snippet of code, illustrates how to put, remove or accumulate values in a JSONObject object, use of JSONWriter for putting key/value pairs in "quick and convenient way" as stated by official documentation, and more practical uses for org.json beginners
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.net.URI; import java.net.URISyntaxException; import org.json.*; /** * @author prgrmmr.aben [at] gmail (dot) com * http://fivesnippets.blogspot.com/2014/09/jsonobject-jsonwriter-and-jsontokener.html * please give back a small donation if you find * this little educational snippet of code useful */ public class test { int intValue=0; public int getIntValue() { return intValue; } public void setIntValue(int intValue) { this.intValue = intValue; } return stringValue; } this.stringValue = stringValue; } return doubleValue; } this.doubleValue = doubleValue; } static URI uri =null; static { //is there a way to create an object which constructor can throw //an exception without using static block?, please answer me in comments try { uri = new URI("https://raw.githubusercontent.com/originals974/symfony/master/composer.json"); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // TODO Auto-generated method stub /* * creation of an URI object consisted of parsing a Json file in this address * creation of two Json objects one by default construct, * the other with a bean as an argument, a bean is an object * with public getters and setters */ JSONObject jsonObj = new JSONObject(); JSONObject jsonObj2 = new JSONObject(new test()); //put key/value pairs and experimenting the toString() method //on Json objects jsonObj.put("value1", "qwerty"); jsonObj.accumulate("value1", "azerty"); //we can put objects of type JSONObject, like we did with beans, //we do with JSONObject objects jsonObj.put("innerJson", jsonObj2); jsonObj.remove("innerJson"); //JSONObject.doubleToString() returns null //when NaN (not finite number) value is passed as its argument //getting Names (keys) of jsonObj2 } //you need to activate assertions, pass -ea as an argument to the JVM /* * making a JSONWriter object to put key/value pairs in "quick and convenient way" * as stated in the documentation of JSONWriter */ JSONWriter JsonWriter = new JSONWriter(writer); JsonWriter.object().key("key1").value(new String("value1")).key("key2").value("value2").key("key3").value("value3").endObject(); /* * experiencing parsing methods in JSONTokener objects * we pass an inputStream after opening a connection to an URL */ StringBuilder builder = new StringBuilder(); while ((aux = reader.readLine()) != null) { builder.append(aux); } jsonStreamExample = uri.toURL().openStream(); JSONTokener tokener = new JSONTokener(jsonStreamExample); System.out.println(" this is the value that comes next to the space after ':' --> "+tokener.nextValue()); } }
以上是关于JSONObjectJSONWriter和JSONTokener类的一些用法组织.json的主要内容,如果未能解决你的问题,请参考以下文章
python中json文件处理涉及的四个函数json.dumps()和json.loads()json.dump()和json.load()的区分
python中json文件处理涉及的四个函数json.dumps()和json.loads()json.dump()和json.load()的区分
python中json文件处理涉及的四个函数json.dumps()和json.loads()json.dump()和json.load()的区分
python中json.dumps()和json.dump() 以及 json.loads()和json.load()的区分