forked from D-Net/dnet-hadoop
Removing prefix c: from json
This commit is contained in:
parent
8bb00add0d
commit
015f6e88df
|
@ -11,6 +11,7 @@ import java.sql.ResultSet;
|
|||
import java.sql.Statement;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
|
@ -187,14 +188,8 @@ public class SarcStats {
|
|||
for (Object aJsonArray : jsonArray) {
|
||||
|
||||
JSONObject jsonObjectRow = (JSONObject) aJsonArray;
|
||||
|
||||
Set<String> jkeys = jsonObjectRow.keySet();
|
||||
for (String jkey : jkeys) {
|
||||
System.out.println("++++> " + jkey);
|
||||
String[] splitArray = jkey.split("c:");
|
||||
jkey = splitArray[splitArray.length - 1];
|
||||
System.out.println("New jkey: " + jkey);
|
||||
}
|
||||
renameKeysRecursively("c:", jsonObjectRow);
|
||||
System.out.println("oooo====> " + jsonObjectRow.toJSONString());
|
||||
|
||||
fin.write(jsonObjectRow.toJSONString().getBytes());
|
||||
fin.writeChar('\n');
|
||||
|
@ -260,6 +255,40 @@ public class SarcStats {
|
|||
|
||||
}
|
||||
|
||||
private void renameKeysRecursively(String prefixToRemove, JSONArray givenJsonObj) throws Exception {
|
||||
for(Object jjval : givenJsonObj) {
|
||||
if(jjval instanceof JSONArray)
|
||||
renameKeysRecursively(prefixToRemove, (JSONArray)jjval);
|
||||
else if(jjval instanceof JSONObject)
|
||||
renameKeysRecursively(prefixToRemove, (JSONObject)jjval);
|
||||
// All other types of vals
|
||||
else;
|
||||
}
|
||||
}
|
||||
|
||||
private void renameKeysRecursively(String prefixToRemove, JSONObject givenJsonObj) throws Exception {
|
||||
Set<String> jkeys = new HashSet<String>(givenJsonObj.keySet());
|
||||
for (String jkey : jkeys) {
|
||||
System.out.println("++++> " + jkey);
|
||||
|
||||
String[] splitArray = jkey.split(prefixToRemove);
|
||||
String newJkey = splitArray[splitArray.length - 1];
|
||||
System.out.println("New jkey: " + jkey);
|
||||
|
||||
Object jval = givenJsonObj.get(jkey);
|
||||
System.out.println("jval ===> " + jval.getClass().getName());
|
||||
givenJsonObj.remove(jkey);
|
||||
givenJsonObj.put(newJkey, jval);
|
||||
|
||||
if(jval instanceof JSONObject)
|
||||
renameKeysRecursively(prefixToRemove, (JSONObject)jval);
|
||||
|
||||
if(jval instanceof JSONArray) {
|
||||
renameKeysRecursively(prefixToRemove, (JSONArray)jval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getJson(String url) throws Exception {
|
||||
// String cred=username+":"+password;
|
||||
// String encoded = new sun.misc.BASE64Encoder().encode (cred.getBytes());
|
||||
|
|
Loading…
Reference in New Issue