Skip to content

Instantly share code, notes, and snippets.

@DogLooksGood
Last active January 4, 2016 02:05
Show Gist options
  • Save DogLooksGood/7d16a79009672966a601 to your computer and use it in GitHub Desktop.
Save DogLooksGood/7d16a79009672966a601 to your computer and use it in GitHub Desktop.
json generate helper
import org.codehaus.jettison.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Created by DogLooksGood on 16/1/3.
*
*/
public class JsonPathHelper {
private static void insert1(JSONObject obj, String[] paths, Object insertion)
throws Exception {
if (paths.length == 1) {
obj.put(paths[0], insertion);
} else if (obj.has(paths[0])) {
insert1(obj.getJSONObject(paths[0]),
Arrays.copyOfRange(paths, 1, paths.length),
insertion);
} else {
JSONObject innerObj = new JSONObject();
insert1(innerObj,
Arrays.copyOfRange(paths, 1, paths.length),
insertion);
obj.put(paths[0], innerObj);
}
}
public static void insert(JSONObject obj, String path, Object insertion)
throws Exception {
String[] paths = path.split("/");
insert1(obj, paths, insertion);
}
private static void objectToArray1(JSONObject obj, String[] paths, String uniqKey)
throws Exception {
boolean done = false;
JSONObject curr = obj;
int i = 0;
for (; i < paths.length - 1; ++i) {
String p = paths[i];
if ("*".equals(p)) {
String[] innerPaths = Arrays.copyOfRange(paths, i + 1, paths.length);
for (Iterator it = curr.keys(); it.hasNext(); ) {
String key = (String) it.next();
objectToArray1(curr.getJSONObject(key), innerPaths, uniqKey);
}
done = true;
} else {
curr = curr.getJSONObject(p);
}
}
if (done) return;
Object origin = curr.get(paths[i]);
JSONObject o = (JSONObject) origin;
List<Object> list = new ArrayList<>();
for (Iterator it = o.keys(); it.hasNext(); ) {
String uniqValue = (String) it.next();
JSONObject objValue = o.getJSONObject(uniqValue);
if (uniqKey != null) {
objValue.put(uniqKey, uniqValue);
}
list.add(objValue);
}
curr.put(paths[i], list);
}
public static void objectToArray(JSONObject obj, String path, String uniqKey)
throws Exception {
String[] paths = path.split("/");
objectToArray1(obj, paths, uniqKey);
}
public static void main(String[] args) throws Exception {
// CASE 01
// 目标:
// {
// "users": [{
// "name": "狗",
// "card": "1000010000",
// "cash": 100
// },
// {
// "name": "猫",
// "card": "1000010001",
// "cash": 200
// }]
// }
// 首先假设整个json都是字典套字典的格式
// {
// "users": {
// "狗": {
// "card": "1000010000",
// "cash": 100
// },
//
// "猫": {
// "card": "1000010001",
// "cash": 200
//
// }
// }
// }
// 这样所有的值都有唯一的路径.按照路径将所有的值放入JSON
JSONObject obj0 = new JSONObject();
insert(obj0, "users/狗/card", "1000010000");
insert(obj0, "users/狗/cash", 100);
insert(obj0, "users/猫/card", "1000010001");
insert(obj0, "users/猫/cash", 200);
// 之后将 users/ 下面的部分,换成Array.在Array的每一个元素中添加一个新的值,key是"name",值是原本Object中的key
objectToArray(obj0, "users/", "name");
System.out.println(obj0.toString());
// 其他的例子....
// Array inside
Item one = new Item("丰台", "100", "狗好看", "m");
JSONObject obj1 = new JSONObject();
for (int i = 0; i < 3; ++i) {
insert(obj1,
String.format("datalist/district/%s/user/%s/code", one.district, i),
one.code);
insert(obj1,
String.format("datalist/district/%s/user/%s/name", one.district, i),
one.name);
}
objectToArray(obj1, "datalist/district/*/user/", null);
System.out.println(obj1.toString());
// Nested Object and Array
List<Item> data = new ArrayList<>();
data.add(new Item("丰台", "100", "狗好看", "m"));
data.add(new Item("朝阳", "101", "叼毛兽", "w"));
data.add(new Item("丰台", "102", "破壳兔", "w"));
JSONObject obj2 = new JSONObject();
for (Item i : data) {
insert(obj2,
String.format("datalist/district/%s/user/%s/code", i.district, i.name),
i.code);
insert(obj2,
String.format("datalist/district/%s/user/%s/gender", i.district, i.name),
i.gender);
}
objectToArray(obj2, "datalist/district/*/user/", "username");
objectToArray(obj2, "datalist/district/", "district_name");
System.out.println(obj2.toString());
}
}
class Item {
String district;
String code;
String name;
String gender;
public Item(String district, String code, String name, String gender) {
this.district = district;
this.code = code;
this.name = name;
this.gender = gender;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment