package com.ustcinfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 2014-3-21
* author:dubingbing
* version:1.0
*/
public class test {
public static void main(String[] args) {
String jsonStr = ""; //& 表示JSON数组
System.out.println(jsonStr);
// String strPath = "ContractRoot/SvcCont/SOO&/QRY_CUSTINFO_SERVER&/CUST_NAME|CUST_ID|CUST_ADDRESS";
// String strPath_second = "ContractRoot/SvcCont/SOO&/CERTIF&/PTY_ID|CERTIF_NUMBER|CERTIF_TYPE_NAME";
String strPath_third = "ContractRoot/SvcCont/SOO&/CERTIF&/FM_QRY_DETAIL&/third&/FIRST_LINE|SECOND_LINE|THIRD_LINE";
List <String> strPaths = new ArrayList<String>();
// strPaths.add(strPath);
// strPaths.add(strPath_second);
strPaths.add(strPath_third);
List nodeString = null;//存放一个路径中各个节点
List<List> valuesList = new ArrayList<List>();//存放所有返回值
List nodeString_son = null;
for(int i=0;i<strPaths.size();i++){
String[] s = strPaths.get(i).split("/");
String[] node = s[s.length-1].split("\\|");
nodeString = new ArrayList();
nodeString_son = new ArrayList();
for(int j =0;j<s.length-1;j++){
nodeString.add(s[j]);
}
for(String temp : node){
nodeString_son.add(temp);//所有路径下的几点名
}
System.out.println(nodeString);
System.out.println(nodeString_son);
List list = json2List1(jsonStr,nodeString,nodeString_son);
valuesList.add(list);
}
//遍历出所有的获取到的节点值
for(int i=0;i<valuesList.size();i++){
List valueList = valuesList.get(i);
for(int j=0;j<valueList.size();j++){
Map keys_values = (Map)valueList.get(j);
System.out.println(keys_values);
}
}
}
/**
*
* @param jsonStr 返回报文的json字符串
* @param nodeString 路径
* @param nodeNames 需要返回那些节点对应的值
* @return
*/
public static List json2List1(String jsonStr,List<String> nodeString,List<String> nodeNames){
List<Map> list = new ArrayList<Map>();//存放对应的节点值
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
List<JSONObject> jsonObjects = null;
List<JSONObject> jsonObjectsTemp = new ArrayList<JSONObject>();
int count =1;
String node = null;
for(int i=0;i<nodeString.size();i++){
node = nodeString.get(i);
if(!node.contains("&")){//正常的JSONObject
if(jsonObject != null){
jsonObject = getJSONObjectInJO(jsonObject, node);
System.out.println(jsonObject);
}else{
for(JSONObject jsonObjectTemp :jsonObjects){
jsonObject = getJSONObjectInJO(jsonObjectTemp, node);
jsonObjectsTemp.add(jsonObject);
}
jsonObjects.clear();
source2target(jsonObjectsTemp,jsonObjects);
jsonObjectsTemp.clear();
}
}else{//是的JSONArray
node = node.replace("&", "");
if(count>1){
for(JSONObject jsonObjectTemp :jsonObjects){
List<JSONObject> temp = getJOFromJOInJA(jsonObjectTemp, node);
for(JSONObject jsonObjectTemp_second :temp){
jsonObjectsTemp.add(jsonObjectTemp_second);
}
}
jsonObjects.clear();
source2target(jsonObjectsTemp,jsonObjects);
jsonObjectsTemp.clear();
}else{
jsonObjects = getJOFromJOInJA(jsonObject, node);//获得ja下的所有jo
}
jsonObject = null;
count++;
}
}
for(JSONObject jsonObjectTemp:jsonObjects){
Map map = getSomeKVsFromJSONObject(jsonObjectTemp, nodeNames);
list.add(map);
}
return list;
}
public static void source2target (List<JSONObject> source , List<JSONObject> target){
for(JSONObject jsonObject :source){
target.add(jsonObject);
}
}
//
// //定义了nodeMap的list放在返回list的最后一个
// public static List json2List(String jsonStr,List<String> nodeString,List<String> nodeNames) {
// int count=1;
// List<Map> list = new ArrayList<Map>();//存放JSONObject
// JSONObject jsonObject = JSONObject.fromObject(jsonStr);
// //根据路径判断获得的JSONOBJECT
// List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
// List<List<JSONObject>> jsonObjects_temp = new ArrayList<List<JSONObject>>();
// for(String key:nodeString){
// if(isGetJSONArray(key)){
// String newKey = key.replace("&", "");
// if(newKey.contains("@")){
// newKey = newKey.replace("@", "");
// jsonObject = getJOFromJOInJA(jsonObject,newKey).get(0);
// System.out.println(jsonObject.toString());
// }else if(newKey.contains("%")){
// newKey = newKey.replace("%", "");
// count++;
//// IF(COUNT ==1){
// for(JSONObject jsonObjectTemp:jsonObjects){
// jsonObjects_temp.add(getJOFromJOInJA(jsonObjectTemp,newKey));
// }
// }else{
// jsonObjects = getJOFromJOInJA(jsonObject,newKey);
// }
// }else{
// jsonObject = getJSONObjectInJO(jsonObject,key);
// System.out.println(jsonObject.toString());
// }
// }
// //
// if(nodeString.get(nodeString.size()-1).contains("@")){//取出某个节点下的所有k_v
// Map obejctMap = getSomeKVsFromJSONObject(jsonObject,nodeNames);
// list.add(obejctMap);
// }else if(nodeString.get(nodeString.size()-1).contains("%")){
// for(List<JSONObject> listTemp:jsonObjects_temp){
// for(JSONObject jsonObjectTemp:listTemp){
// Map obejctMap = getSomeKVsFromJSONObject(jsonObjectTemp,nodeNames);
// list.add(obejctMap);
// }
// }
// }else{
// for(JSONObject jsonObjectTemp:jsonObjects){
// Map obejctMap = getSomeKVsFromJSONObject(jsonObjectTemp,nodeNames);
// list.add(obejctMap);
// }
// }
// return list;
// }
//根据父类的JSONObject来获取子类的JSONObject对象
public static JSONObject getJSONObjectInJO(JSONObject parentJsonObject,String key){
JSONObject jsonObject = parentJsonObject.getJSONObject(key);
return jsonObject;
}
//根据父类的JSONObject来获取子类的JSONArray对象
public static void getJSONArray(JSONObject parentJsonObject,String key){
JSONArray jsonArray = parentJsonObject.getJSONArray(key);
for(int i=0;i<jsonArray.size();i++){
JSONObject jsonObject_second = jsonArray.getJSONObject(i);
Iterator iterator_second = jsonObject_second.keys();
while(iterator_second.hasNext()){
String key_second = (String)iterator_second.next();
String value_second = jsonObject_second.getString(key_second);
System.out.print("key_jsonArray---"+key_second);
System.out.println(",value_jsonArray---"+value_second);
}
}
}
/**
* parentJO 父类的JSONOBJECT
* key
* 根据key获得JSONOBJECT的对应JSONArray
* 父类的JSONOBJECT获得子JSONArray,在从JSONArray获得JSONOBJECT
*/
public static List<JSONObject> getJOFromJOInJA(JSONObject parentJO,String key){
List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
JSONObject jsonObject =null;
JSONArray jsonArray = parentJO.getJSONArray(key);
for(int i=0;i<jsonArray.size();i++){
jsonObject = jsonArray.getJSONObject(i);
jsonObjects.add(jsonObject);
}
return jsonObjects;
}
//根据JSONObject对象迭代出所有的key和value
public static Map getKVFromJSONObject(JSONObject jsonObject){
Map map = new HashMap();
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
String key = (String)iterator.next();
String value = jsonObject.getString(key);
map.put(key, value);
System.out.print("key---"+key);
System.out.println(",value---"+value);
}
return map;
}
//根据JSONOBject对象返回指定keys下的key和value值
public static Map getSomeKVsFromJSONObject(JSONObject jsonObject,List<String> keys){
Map map = new HashMap();
for(String key:keys){
String value = jsonObject.getString(key);
map.put(key, value);
}
return map;
}
//根据JSONOBject对象返回指定key下的key和value值
public static Map getOneKVFromJSONObject(JSONObject jsonObject,String key){
Map map = new HashMap();
String value = jsonObject.getString(key);
map.put(key, value);
return map;
}
//根据该key下面返回的是JSONObject 还是JSONArray &表示JSONArray
public static boolean isGetJSONArray(String key){
boolean flag = false;
if(key.contains("&")){
flag = true;
}
return flag;
}
//判断是否指需要JSONOBJECT下面的key和value值
public static boolean isGetKeyValue(String key){
boolean flag = false;
if(key.contains("*")){
flag = true;
}
return flag;
}
}