`
pharaohsprince
  • 浏览: 289121 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

JSON详细学习之JSONObject in JAVA

阅读更多

一、JAR包简介

要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:

1.commons-lang.jar

2.commons-beanutils.jar

3.commons-collections.jar

4.commons-logging.jar

5.ezmorph.jar

6.json-lib-2.2.2-jdk15.jar

二、 JSONObject对象使用

JSON- lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包。在本例中,我们将使用JSONObject类创建JSONObject对象,然后我们打印这些对象的值。为了使用 JSONObject对象,我们要引入"net.sf.json"包。为了给对象添加元素,我们要使用put()方法。

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1262010943179" flashvars="clipboard=import%20net.sf.json.JSONArray%3B%0Aimport%20net.sf.json.JSONObject%3B%0A%0Apublic%20class%20JSONObjectSample%20%7B%0A%09%0A%09%2F%2F%E5%88%9B%E5%BB%BAJSONObject%E5%AF%B9%E8%B1%A1%0A%09private%20static%20JSONObject%20createJSONObject()%7B%0A%09%09JSONObject%20jsonObject%20%3D%20new%20JSONObject()%3B%0A%09%09jsonObject.put(%22name%22%2C%20%22kevin%22)%3B%0A%09%09jsonObject.put(%22Max.score%22%2C%20new%20Integer(100))%3B%0A%09%09jsonObject.put(%22Min.score%22%2C%20new%20Integer(50))%3B%0A%09%09jsonObject.put(%22nickname%22%2C%20%22picglet%22)%3B%0A%09%09return%20jsonObject%3B%0A%09%7D%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09JSONObject%20jsonObject%20%3D%20JSONObjectSample.createJSONObject()%3B%0A%09%09%2F%2F%E8%BE%93%E5%87%BAjsonobject%E5%AF%B9%E8%B1%A1%0A%09%09System.out.println(%22jsonObject%3D%3D%3E%22%2BjsonObject)%3B%0A%09%09%0A%09%09%2F%2F%E5%88%A4%E8%AF%BB%E8%BE%93%E5%87%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%B1%BB%E5%9E%8B%0A%09%09boolean%20isArray%20%3D%20jsonObject.isArray()%3B%0A%09%09boolean%20isEmpty%20%3D%20jsonObject.isEmpty()%3B%0A%09%09boolean%20isNullObject%20%3D%20jsonObject.isNullObject()%3B%0A%09%09System.out.println(%22isArray%3A%22%2BisArray%2B%22%20isEmpty%3A%22%2BisEmpty%2B%22%20isNullObject%3A%22%2BisNullObject)%3B%0A%09%09%0A%09%09%2F%2F%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7%0A%09%09jsonObject.element(%22address%22%2C%20%22swap%20lake%22)%3B%0A%09%09System.out.println(%22%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7%E5%90%8E%E7%9A%84%E5%AF%B9%E8%B1%A1%3D%3D%3E%22%2BjsonObject)%3B%0A%09%09%0A%09%09%2F%2F%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AAJSONArray%E5%AF%B9%E8%B1%A1%0A%09%09JSONArray%20jsonArray%20%3D%20new%20JSONArray()%3B%0A%09%09jsonArray.add(0%2C%20%22this%20is%20a%20jsonArray%20value%22)%3B%0A%09%09jsonArray.add(1%2C%22another%20jsonArray%20value%22)%3B%0A%09%09jsonObject.element(%22jsonArray%22%2C%20jsonArray)%3B%0A%09%09JSONArray%20array%20%3D%20jsonObject.getJSONArray(%22jsonArray%22)%3B%0A%09%09System.out.println(%22%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AAJSONArray%E5%AF%B9%E8%B1%A1%EF%BC%9A%22%2Barray)%3B%0A%09%09%2F%2F%E6%B7%BB%E5%8A%A0JSONArray%E5%90%8E%E7%9A%84%E5%80%BC%0A%09%09%2F%2F%7B%22name%22%3A%22kevin%22%2C%22Max.score%22%3A100%2C%22Min.score%22%3A50%2C%22nickname%22%3A%22picglet%22%2C%22address%22%3A%22swap%20lake%22%2C%0A%09%09%2F%2F%22jsonArray%22%3A%5B%22this%20is%20a%20jsonArray%20value%22%2C%22another%20jsonArray%20value%22%5D%7D%0A%09%09System.out.println(jsonObject)%3B%0A%09%09%0A%09%09%2F%2F%E6%A0%B9%E6%8D%AEkey%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%0A%09%09String%20jsonString%20%3D%20jsonObject.getString(%22name%22)%3B%0A%09%09System.out.println(%22jsonString%3D%3D%3E%22%2BjsonString)%3B%0A%09%7D%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. import net.sf.json.JSONArray;
  2. import net.sf.json.JSONObject;
  3. public class JSONObjectSample{
  4. //创建JSONObject对象
  5. private static JSONObjectcreateJSONObject(){
  6. JSONObjectjsonObject=new JSONObject();
  7. jsonObject.put("name" , "kevin" );
  8. jsonObject.put("Max.score" , new Integer( 100 ));
  9. jsonObject.put("Min.score" , new Integer( 50 ));
  10. jsonObject.put("nickname" , "picglet" );
  11. return jsonObject;
  12. }
  13. public static void main(String[]args){
  14. JSONObjectjsonObject=JSONObjectSample.createJSONObject();
  15. //输出jsonobject对象
  16. System.out.println("jsonObject==>" +jsonObject);
  17. //判读输出对象的类型
  18. boolean isArray=jsonObject.isArray();
  19. boolean isEmpty=jsonObject.isEmpty();
  20. boolean isNullObject=jsonObject.isNullObject();
  21. System.out.println("isArray:" +isArray+ "isEmpty:" +isEmpty+ "isNullObject:" +isNullObject);
  22. //添加属性
  23. jsonObject.element("address" , "swaplake" );
  24. System.out.println("添加属性后的对象==>" +jsonObject);
  25. //返回一个JSONArray对象
  26. JSONArrayjsonArray=new JSONArray();
  27. jsonArray.add(0 , "thisisajsonArrayvalue" );
  28. jsonArray.add(1 , "anotherjsonArrayvalue" );
  29. jsonObject.element("jsonArray" ,jsonArray);
  30. JSONArrayarray=jsonObject.getJSONArray("jsonArray" );
  31. System.out.println("返回一个JSONArray对象:" +array);
  32. //添加JSONArray后的值
  33. //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swaplake",
  34. //"jsonArray":["thisisajsonArrayvalue","anotherjsonArrayvalue"]}
  35. System.out.println(jsonObject);
  36. //根据key返回一个字符串
  37. StringjsonString=jsonObject.getString("name" );
  38. System.out.println("jsonString==>" +jsonString);
  39. }
  40. }

得到JSONObject对象后我们就可以使用它的方法了,可以查看其API,我给出一个在线的API

http://json-lib.sourceforge.net/apidocs/jdk15/index.html

分享到:
评论

相关推荐

    org.json.JSONObject jar包

    org.json.JSONObject的6个jar包 commons-beanutils;commons-collections;commons-lang;commons-logging;ezmorph;json-lib

    Json详细学习in Java

    集成了JSONObject,JackSon,Gson这3种技术对json的操作,能方便我们的学习,相信能让开发者更快地上手.

    Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject,是因为缺少java-json.jar-附件资源

    Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject,是因为缺少java-json.jar-附件资源

    java-json.jar

    sqoop1 import 时报错Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject。下载后,然后放到sqoop/lib目录即可。

    java-json.rar

    sqoop import 时报错Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject。下载后,然后放到sqoop/lib目录即可。

    Android代码-ason

    This library intends to make JSON very easy to interact with in Java; it also makes (de)serialization painless. It wraps around the well-known org.json classes (JSONObject, JSONArray, etc.) which also...

    Minced:将 JSON 键转换为驼峰式大小写

    // Converts all the keys in the JSON to camelCase - ( id )minced_JSONKeys; - ( NSArray *)minced_JSONObjectsKeys; - ( NSDictionary *)minced_JSONObjectKeys; // Converts all the keys in the JSON to ...

    网络获取json解析json以及子线程显示

    import org.json.JSONObject; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import java.io....

    fast-json:快速轻巧的JSON解析器和创建器

    try (JSONInputStream in = new JSONInputStream(json)) {obj = in.readObject();} catch (IOException | JSONException e) {e.printStackTrace();}第三是使用输入流,例如文件: JSONObject obj;try (JSONInputStr

    weChatpay完整版java

    JSONObject jsonObject = JSONObject.fromObject(json); // 如果请求成功 if (null != jsonObject) { try { accessToken = new UserAccessToken(); accessToken.setAccessToken(jsonObject....

    Java_tools:在Java中使用URL和JSON的工具(任意路径访问而无需了解整个架构)

    允许在某个路径Abcd上进行任意引用,而无需了解整个架构-gJsonReader -getAtt(Object jsonObject, String path) /** * * @param jsonObject - Json String or LinkedTreeMap (which are returned in an ArrayList ...

    详解Java中String JSONObject JSONArray List实体类转换

    JSON使用阿里的fastJson为依赖包 gradle依赖管理如下: compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41' 1、String转JSONObject 前言:String 是JSONObject格式的字符串 eg: JSONObject ...

    Android代码-WebViewJavaScriptBridge

    规定JS和Java之间用标准JSON格式字符串交互,JS传给Java的数据会封装成 org.json.JSONObject。 (An Android bridge for sending messages between Java and JavaScript in WebViews. Based on IOS marcuswestin/...

    Android静默安装常用工具类

    源码可见HttpUtils.java,更多方法及更详细参数介绍可见HttpUtils Api Guide。 2、DownloadManagerPro Android系统下载管理DownloadManager增强方法,可用于包括获取下载相关信息,如: getStatusById(long) 得到...

    XMLValidation:带有模式和schematron的电子邮件XML验证程序的电子税收发票

    XML验证项目基于Javax.xml.validation...// get response massage in JSONObject (JSON Simple lib)JSONObject responseMessage = ctrl.Validate(XML_test);// get response massage in JSONObject (JSON Simple lib)JS

    Sqoop 导入数据异常处理

    写在前面 ...解压使用会出现几个bug,之前也是出现了,... Exception in thread “main” java.lang.NoClassDefFoundError: org/json/JSONObject 解决方法: 这是因为sqoop缺少java-json.jar包 下载java-json.jar包: http

    HttpToolbox:HTTP 工具箱

    HTTP 工具箱v0.1.0 2014-12-19用途列举了HttpClient的常用功能, 让我们可以快速上手完成发送HTTP请求...), 还可以Make a prettyprinted JSON text of JSONObject(fastjson也有这个功能, 但是不理想, 输出的JSON是通过ta

    ORM软件monalisa-orm.zip

    //parse data from type: Map, json/xml string, JsonObject(Gson) //, HttpServletRequest, JavaBean new User().parse("{'name':'oschina','status':0}").save(); new User().parse("&lt;data&gt; &lt;name&gt;china01&lt;/name&gt;...

Global site tag (gtag.js) - Google Analytics