关于人人网的json数据的问题

前几天看了http://www.iteye.com/topic/826988关于抓取人人网的数据的问题,但是看到http://s.xnimg.cn/a13819/allunivlist.js这个js文件上的json数据,小弟想请问一下,这样的json数据时怎么样生成的,貌似俺以前做json数据生成的时候,如果有Collection类型的数据就会报错,希望了解这样的问题的童鞋给点提示,或者是直接上代码,谢谢......如果用json-lib,代码要怎么样写?

如果在College中设置了Province属性,或是在Depart中设置了College属性,就会报错

这是由于在生成json字符串时,由于College包含了Province,Province又包含了College,造成了循环引用

解决方法:
在上面的过滤器配置JsonConfig中添加:
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

json-lib使用例子:
[code="java"]
package json.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import json.bean.MyBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FirstTest {

/**
 * 简化System.out.println
 * 
 * @param obj
 */
public static void out(Object obj) {
    System.out.println(obj);
}

/**
 * @param args
 */
public static void main(String[] args) {
    // boolean数组
    boolean[] boolArray = new boolean[] { true, false, true };
    JSONArray jsonArray = JSONArray.fromObject(boolArray);
    out(boolArray + "结果:" + jsonArray);

    // List
    List<String> list = new ArrayList<String>();
    list.add("first");
    list.add("second");
    JSONArray jsonArray1 = JSONArray.fromObject(list);
    out(list + "结果:" + jsonArray1);

    // String[]
    JSONArray jsonArray2 = JSONArray.fromObject("['json','is','easy']");
    out(jsonArray2);

    // Map
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "json");
    map.put("bool", Boolean.TRUE);
    map.put("int", new Integer(1));
    map.put("arr", new String[] { "a", "b" });
    map.put("func", "function(i){ return this.arr[i]; }");
    JSONObject jsonObject = JSONObject.fromObject(map);
    out(jsonObject);

    // Bean
    JSONObject jsonObject1 = JSONObject.fromObject(new MyBean());
    out("Bean:" + jsonObject1);
}

}

[/code]

[code="java"]
package json.bean;

import net.sf.json.JSONFunction;

public class MyBean {
private String name = "json";

private int pojoId = 1;

private char[] options = new char[] { 'a', 'f' };

private String func1 = "function(i){ return this.options[i]; }";

private JSONFunction func2 = new JSONFunction(new String[] { "i" }, "return this.options[i];");

public String getFunc1() {
    return func1;
}

public void setFunc1(String func1) {
    this.func1 = func1;
}

public JSONFunction getFunc2() {
    return func2;
}

public void setFunc2(JSONFunction func2) {
    this.func2 = func2;
}

public String getName() {
    return name;
}

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

public char[] getOptions() {
    return options;
}

public void setOptions(char[] options) {
    this.options = options;
}

public int getPojoId() {
    return pojoId;
}

public void setPojoId(int pojoId) {
    this.pojoId = pojoId;
}

}
[/code]

这个不难,在json-lib 中有几种对象 JSONObject JSONArray这两个最关键,一种是转换为json对象,一种是json数组,既:
1。[{"a":"1","b":"2"},{"a":"2","b":"3"}]
2. {"a":"1","b":"2"}

如果你想collection到json转换,首先你的collection结构要与json对应上
{...}这样是一个对象,在js中。但是它同样在java中等同一个对象,所以你要建立一个java对象来符合他,拿{"a":"1","b":"2"}来说,你得先建立一个java对象
[code="java"]
public class A{
private string a;
private string b;
...
}
//这里注意只有有getter setter方法的属性才可以转换为json中的属性

[/code]

[code="java"]
A abc = new A("1","2");
JSONObject obj = JSONObject.fromObject(abc);
//obj = {"a":"1","b":"2"}
[/code]

如果你要一个层级的比如:
{"a":1,obj:{"b":3}};
那么对象得这么建
[code="java"]
public class A{
private int a;
private C obj; //这里的名称跟json生成的格式中的属性名称是一样的
...
}
class C{
private int b;
...
}

C c = new C(3);
A a = new A(1,c);
JSONObject obj = JSONObject.fromObject(A);
//结果:{"a":1,obj:{"b":3}};

[/code]

这里说说
[{"a":"1","b":"2"},{"a":"2","b":"3"}]

这是JSONArray格式,可以通过:
[code="java"]
List list = new ArrayList();
...

JSONArray arry = JSONArray.fromObject(list);
[/code]

net.sf.json.util.PropertyFilter可以实现对属性的过滤:
可以参考:
http://wujuncheng.iteye.com/blog/543763

过滤为值为null的属性,代码如下:
[code="java"]
import net.sf.json.util.PropertyFilter;

/**

  • 自定义属性过滤器
  • @author qiuzj
    *
    */
    public class CustomPropertyFilter implements PropertyFilter {

    @Override
    public boolean apply(Object source, String name, Object value) {
    // 过滤掉为null的属性
    if (value == null)
    return true;
    return false;
    }

}

[/code]

[code="java"]
import java.util.HashSet;
import java.util.Set;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class Test1 {

/**
 * @param args
 */
public static void main(String[] args) {
    Test1 test = new Test1();
    test.testFormat();
}

public void testFormat() {
    Province pro = new Province();
    pro.setProid(12);
    pro.setProname("湖北省");
    College col1 = new College();
    col1.setColid(122);
    col1.setName("黄冈师范");
    College col2 = new College();
    col2.setColid(123);
    col2.setName("黄冈师范学院");
    Department dept1 = new Department();
    dept1.setDepid(1224);
    dept1.setDepname("计科院");
    Department dept2 = new Department();
    dept2.setDepid(1225);
    dept2.setDepname("计科");
    Set<Department> depts = new HashSet<Department>();
    depts.add(dept1);
    depts.add(dept2);
    col1.setDepts(depts);
    col2.setDepts(depts);
    Set<College> cols = new HashSet<College>();
    cols.add(col1);
    cols.add(col2);
    pro.setColleges(cols);

    // 自定义过滤器
    JsonConfig config = new JsonConfig();
    config.setJsonPropertyFilter(new CustomPropertyFilter());

    JSONObject json = JSONObject.fromObject(pro, config);

    System.out.println(json.toString());
}

}

[/code]