一、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>
-
import
net.sf.json.JSONArray;
-
import
net.sf.json.JSONObject;
-
-
public
class
JSONObjectSample{
-
-
-
private
static
JSONObjectcreateJSONObject(){
-
JSONObjectjsonObject=new
JSONObject();
-
jsonObject.put("name"
,
"kevin"
);
-
jsonObject.put("Max.score"
,
new
Integer(
100
));
-
jsonObject.put("Min.score"
,
new
Integer(
50
));
-
jsonObject.put("nickname"
,
"picglet"
);
-
return
jsonObject;
-
}
-
public
static
void
main(String[]args){
-
JSONObjectjsonObject=JSONObjectSample.createJSONObject();
-
-
System.out.println("jsonObject==>"
+jsonObject);
-
-
-
boolean
isArray=jsonObject.isArray();
-
boolean
isEmpty=jsonObject.isEmpty();
-
boolean
isNullObject=jsonObject.isNullObject();
-
System.out.println("isArray:"
+isArray+
"isEmpty:"
+isEmpty+
"isNullObject:"
+isNullObject);
-
-
-
jsonObject.element("address"
,
"swaplake"
);
-
System.out.println("添加属性后的对象==>"
+jsonObject);
-
-
-
JSONArrayjsonArray=new
JSONArray();
-
jsonArray.add(0
,
"thisisajsonArrayvalue"
);
-
jsonArray.add(1
,
"anotherjsonArrayvalue"
);
-
jsonObject.element("jsonArray"
,jsonArray);
-
JSONArrayarray=jsonObject.getJSONArray("jsonArray"
);
-
System.out.println("返回一个JSONArray对象:"
+array);
-
-
-
-
System.out.println(jsonObject);
-
-
-
StringjsonString=jsonObject.getString("name"
);
-
System.out.println("jsonString==>"
+jsonString);
-
}
-
}
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONObjectSample {
//创建JSONObject对象
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kevin");
jsonObject.put("Max.score", new Integer(100));
jsonObject.put("Min.score", new Integer(50));
jsonObject.put("nickname", "picglet");
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
//输出jsonobject对象
System.out.println("jsonObject==>"+jsonObject);
//判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject);
//添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>"+jsonObject);
//返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println("返回一个JSONArray对象:"+array);
//添加JSONArray后的值
//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject);
//根据key返回一个字符串
String jsonString = jsonObject.getString("name");
System.out.println("jsonString==>"+jsonString);
}
}
得到JSONObject对象后我们就可以使用它的方法了,可以查看其API,我给出一个在线的API
http://json-lib.sourceforge.net/apidocs/jdk15/index.html
分享到:
相关推荐
org.json.JSONObject的6个jar包 commons-beanutils;commons-collections;commons-lang;commons-logging;ezmorph;json-lib
集成了JSONObject,JackSon,Gson这3种技术对json的操作,能方便我们的学习,相信能让开发者更快地上手.
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject,是因为缺少java-json.jar-附件资源
sqoop1 import 时报错Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject。下载后,然后放到sqoop/lib目录即可。
sqoop import 时报错Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject。下载后,然后放到sqoop/lib目录即可。
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...
// 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 ...
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....
try (JSONInputStream in = new JSONInputStream(json)) {obj = in.readObject();} catch (IOException | JSONException e) {e.printStackTrace();}第三是使用输入流,例如文件: JSONObject obj;try (JSONInputStr
JSONObject jsonObject = JSONObject.fromObject(json); // 如果请求成功 if (null != jsonObject) { try { accessToken = new UserAccessToken(); accessToken.setAccessToken(jsonObject....
允许在某个路径Abcd上进行任意引用,而无需了解整个架构-gJsonReader -getAtt(Object jsonObject, String path) /** * * @param jsonObject - Json String or LinkedTreeMap (which are returned in an ArrayList ...
JSON使用阿里的fastJson为依赖包 gradle依赖管理如下: compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41' 1、String转JSONObject 前言:String 是JSONObject格式的字符串 eg: JSONObject ...
规定JS和Java之间用标准JSON格式字符串交互,JS传给Java的数据会封装成 org.json.JSONObject。 (An Android bridge for sending messages between Java and JavaScript in WebViews. Based on IOS marcuswestin/...
源码可见HttpUtils.java,更多方法及更详细参数介绍可见HttpUtils Api Guide。 2、DownloadManagerPro Android系统下载管理DownloadManager增强方法,可用于包括获取下载相关信息,如: getStatusById(long) 得到...
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
写在前面 ...解压使用会出现几个bug,之前也是出现了,... Exception in thread “main” java.lang.NoClassDefFoundError: org/json/JSONObject 解决方法: 这是因为sqoop缺少java-json.jar包 下载java-json.jar包: http
HTTP 工具箱v0.1.0 2014-12-19用途列举了HttpClient的常用功能, 让我们可以快速上手完成发送HTTP请求...), 还可以Make a prettyprinted JSON text of JSONObject(fastjson也有这个功能, 但是不理想, 输出的JSON是通过ta
//parse data from type: Map, json/xml string, JsonObject(Gson) //, HttpServletRequest, JavaBean new User().parse("{'name':'oschina','status':0}").save(); new User().parse("<data> <name>china01</name>...