java使用json

int32位 posted @ Jul 31, 2013 01:07:08 PM , 1679 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

首先下载源码: 

git clone https://github.com/douglascrockford/JSON-java

这个git居然没有build.xml, 编译很麻烦, 得自己写个build.xml. 我的build.xml为

<project basedir="." name="json" default="release">
	<property name="src.dir" value="src"/>
	<property name="build.dir" value="build"/>
	<property name="class.dir" value="${build.dir}/classes"/>
	<property name="jar.dir" value="${build.dir}/jar"/>
	<property name="lib.dir" value="lib"/>
	<target name="prepare">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${class.dir}"/>
		<mkdir dir="${jar.dir}"/>
		<mkdir dir="${lib.dir}"/>
	</target>
	<target name="compile" depends="prepare">
		<javac srcdir="${src.dir}" destdir="${class.dir}"/>
	</target>
	<target name="jar" depends="compile">
		<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
		</jar>
	</target>
	<target name="clean" depends="prepare, compile, jar">
		<delete dir="${build.dir}"/>
	</target>
	<target name="release" depends="jar">
		<copy file="${jar.dir}/${ant.project.name}.jar" todir="${lib.dir}"/>
	</target>
</project>

记得把这个build.xml放入JSON的根目录下,然后运行ant就编译好了. 下面测试下 新建一个目录test, 在test下有目录src, lib, 把刚刚编译的json.jar 复制到这个lib下. 然后写一个测试java文件放入src下,代码为:

 

import java.util.*;
import java.io.*;
import org.json.*;
class Bean {
	private int id;
	private String name;
	public Bean(){
		id = 0;
		name = "Mary";
	}
	public int getId() {

		return id;
	}
	public String getName() {

		return name;
	}
	public void setId(int id) {

		this.id = id;
	}
	public void setName(String name) {

		this.name = name;
	}
	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append(id);
		s.append(":");
		s.append(name);
		return s.toString();
	}
}
public class Test {

	public static void main(String[] args) throws Exception {

		String s = new JSONObject().put("JSON", "Hello, World!").toString();
		System.out.println(s);

		Bean b = new Bean();
		b.setId(100);
		b.setName("Mary");
		System.out.println(new JSONObject(b));

		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("b", 2);
		map.put("c", 3);
		JSONObject json = new JSONObject(map);
		json.accumulate("c", 4);
		System.out.println(json);
		json = new JSONObject("{'a':{'a':4}}");
		System.out.println(json);
		JSONStringer js = new JSONStringer();
		System.out.println(js.object().key("name").value("Mary").endObject());
        }
}

在test根目录下写一个build.xml, 内容如下:

<project name="Test" basedir="." default="run">
	<property name="build.dir" value="build"/>
	<property name="lib.dir" value="lib"/>
	<property name="src.dir" value="src"/>
	<property name="class.dir" value="${build.dir}/classes"/>
	<property name="jar.dir" value="${build.dir}/jar"/>
	<property name="main-class" value="Test"/>
	<path id="classpath">
		<fileset dir="${lib.dir}" includes="**/*.jar"/>
	</path>
	<target name="prepare">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${class.dir}"/>
		<mkdir dir="${jar.dir}"/>
	</target>
	<target name="compile" depends="prepare">
		<javac srcdir="${src.dir}" destdir="${class.dir}" classpathref="classpath"/>
	</target>
	<target name="jar" depends="compile">
		<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
			<manifest>
				<attribute name="Main-Class" value="${main-class}"/>
			</manifest>
		</jar>
	</target>
	<target name="run" depends="jar">
		<java fork="true" classname="${main-class}">
			<classpath>
				<path refid="classpath"/>
				<path location="${jar.dir}/${ant.project.name}.jar"/>
			</classpath>
		</java>
	</target>
</project>

运行ant就可以看到结果了... json包中两个最常用的就是JSONObject和JSONArray对象,望文生义,不需解释.

另外JSONStringer 和JSONTokener 也是有用的工具,具体实例传送门:http://blog.csdn.net/xiazdong/article/details/7059573

另外有个问题, JSONObject有一个构造方法是可以把一个java bean转化为JSONObject对象,我的bean为:

class Bean {
	private int id;
	private String name;
	public Bean(){
		id = 0;
		name = "Mary";
	}
	public int getId() {

		return id;
	}
	public String getName() {

		return name;
	}
	public void setId(int id) {

		this.id = id;
	}
	public void setName(String name) {

		this.name = name;
	}
	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append(id);
		s.append(":");
		s.append(name);
		return s.toString();
	}
} 

new 一个Bean后,传进去,不知为什么返回的是{},即一个空json, 不知道什么原因. 

转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
Clay Lowe 说:
2024年9月03日 00:59

Acknowledges for penmanship such a worthy column, I stumbled beside your blog besides predict a handful advise. I want your tone of manuscript... Kissimmee Termite Treatment

Clay Lowe 说:
2024年9月03日 01:09

I should say only that its awesome! The blog is informational and always produce amazing things. Orlando Bed Bug Treatment

Clay Lowe 说:
2024年9月03日 01:09

It is rather very good, nevertheless glance at the data with this handle. Oviedo Bed Bug Treatment

Clay Lowe 说:
2024年10月08日 21:15

Beaver says I also have such interest, you can read my profile here:  فني كاميرات مراقبة الكويت

Clay Lowe 说:
2024年10月10日 14:20

So lot to occur over your amazing blog. Your blog procures me a fantastic transaction of enjoyable.. Salubrious lot beside the scene. mineola CPA Firm

Clay Lowe 说:
2024年10月16日 21:30

This is important, though it's necessary to help you head over to it weblink: فتح ابواب


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter