Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中JSON到JSON的转换_Java_Json_Jackson_Gson_Jolt - Fatal编程技术网

Java中JSON到JSON的转换

Java中JSON到JSON的转换,java,json,jackson,gson,jolt,Java,Json,Jackson,Gson,Jolt,我正试图根据运行时给出的规范在java中实现JSON到JSON的转换。 示例:如果在运行时源代码为:com.gsdetails.gname,目标代码为:com.track.trackName(即源字段应映射到生成的JSON中的目标字段) 我的方法是为规范部分创建N-array树,并执行广度优先travesal(使用它获取队列以创建json的结构) 我使用Jackson api从输入JSON创建树,并遍历队列(bfs)和输入树来创建结果JSON 无法获得预期的输出 PS:我曾想过使用JOLT ap

我正试图根据运行时给出的规范在java中实现JSON到JSON的转换。

示例:如果在运行时源代码为:com.gsdetails.gname,目标代码为:com.track.trackName(即源字段应映射到生成的JSON中的目标字段)

我的方法是为规范部分创建N-array树,并执行广度优先travesal(使用它获取队列以创建json的结构) 我使用Jackson api从输入JSON创建树,并遍历队列(bfs)和输入树来创建结果JSON

无法获得预期的输出

PS:我曾想过使用JOLT api,但它不符合我的目的

树(用于规范) }

类三节点{ 字符串源;/ /将此视为树的内容/引用点 字符串目标; 布尔isEnd; 整数计数; 儿童名单; 布尔isRoot

/* Constructor */
public Trie() {
    root = new TrieNode("", "");
}

public void insertWord(String sourceWords, String targetWords) {
    if (searchWord(sourceWords) == true)
        return;
    TrieNode current = root;

    String[] sourceArray = sourceWords.split(":");
    String[] targetArray = targetWords.split(":");

    for (int i = 0; i < sourceArray.length; i++) {
        TrieNode child = current.subNodeWord(sourceArray[i]);
        if (child != null) {
            current = child;
        } else {
            current.childList.add(new TrieNode(sourceArray[i],
                    targetArray[i]));
            current = current.subNodeWord(sourceArray[i]);
        }
        current.count++;
    }
    current.isEnd = true;
}

public boolean searchWord(String words) {
    TrieNode current = root;
    for (String word : words.split(":")) {
        if (current.subNodeWord(word) == null) {
            return false;
        } else {
            current = current.subNodeWord(word);
        }
    }

    if (current.isEnd == true)
        return true;
    return false;
}

public Queue<TrieNode> bfsTraversal(TrieNode node) {
    // TODO need to add logic for bfs/dfs for traversing the trie
    Queue<TrieNode> queue = new LinkedList<>();
    Queue<TrieNode> tempQueue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        TrieNode tempNode = queue.poll();
        tempQueue.add(tempNode);
        int counter = tempNode.childList.size(), i = 0;
        if (tempNode == null)
            break;
        if (!tempNode.source.isEmpty())
            System.out.println("Source :" + tempNode.source
                    + "     Target : " + tempNode.target);
        while (i < counter) {
            queue.add(tempNode.childList.get(i++));
        }
    }
    tempQueue.poll();
    return tempQueue;
}
/*构造函数*/
公共三节点(字符串源、字符串目标){
childList=newarraylist();
isEnd=假;
计数=0;
this.source=源;
this.target=目标;
}
公共三节点子节点字(字符串字){
if(childList!=null){
for(每三个孩子:孩子列表)
if(每个子源等于(字))
返回每个孩子;
}
返回null;
}
}

三类{ 公共三极根


    source = com:track:trackDetails:fname, target = gsUser:gsProp:gsDetails:gsFirstName
    source = com:track:trackDetails:lname, target = gsUser:gsProp:gsDetails:gsLastName
/*构造函数*/
公共图书馆{
根=新的三元组(“,”);
}
公共void插入字(字符串源字、字符串目标字){
if(searchWord(sourceWords)==true)
返回;
三极电流=根;
String[]sourceArray=sourceWords.split(“:”);
字符串[]targetArray=targetWords.split(“:”);
for(int i=0;i

源到目标映射文件:

public class JsonHelperClass{

//  private Files file = null;// create a tempfile
    private JsonNodeFactory factory;
    private JsonFactory jsonFactory;
    private ObjectMapper mapper;
    private JsonNode jsonRoot;
    private Queue<TrieNode> queue;
//  private JsonParser jsonParser =  

    public JsonHelperClass() throws JsonProcessingException, IOException {
        this.factory = JsonNodeFactory.instance;
        this.jsonFactory  = new JsonFactory();
        this.mapper  = new ObjectMapper();
        this.jsonRoot = mapper.readTree(new File("json with data"));
    }

    public static void main(String[] args) throws Exception, Exception {
            JsonHelperClass helperClass = new JsonHelperClass();
            helperClass.jsonCreator();
            ObjectNode objectNode = null; 
            ObjectNode result = helperClass.createJsonRecursively(objectNode);
            System.out.println(result.toString());

    }

    public void jsonCreator() throws Exception {
        Trie trie = TrieBuilder.createSpec();
        queue = trie.bfsTraversal(trie.root);

    }

    public ObjectNode createJsonRecursively(ObjectNode outputJson) throws Exception {
        TrieNode nodeOfQueue = queue.poll();

        if(outputJson == null){
            // create a root of the JSON
            outputJson = factory.objectNode();
            outputJson.put(nodeOfQueue.target, createJsonRecursively(outputJson));


        }else if (jsonRoot.get(nodeOfQueue.source).isObject()){
            // create an object to conatin other values/object
            ObjectNode objectNode = factory.objectNode();
            objectNode.put(nodeOfQueue.target,createJsonRecursively(outputJson));
            outputJson.putAll(objectNode);


        }else if(jsonRoot.get(nodeOfQueue.source).isArray()){
            // create an array node and call for to create value it contains
            ArrayNode arrayNode = factory.arrayNode();
            int size = jsonRoot.get(nodeOfQueue.source).size();
            for(int index = 0 ; index < size ; index++){
                arrayNode.add(jsonRoot.get(nodeOfQueue.source).get(index));
            }
            outputJson.put(nodeOfQueue.target,arrayNode);           
        }else if(nodeOfQueue.isEnd){
            // create leaf node
            outputJson.put(nodeOfQueue.target, jsonRoot.get(nodeOfQueue.source));
            return outputJson;

        }
        return outputJson;
    }

source=com:track:trackDetails:fname,target=gsUser:gsProp:gsDetails:gsFirstName
source=com:track:trackDetails:lname,target=gsUser:gsProp:gsDetails:gsLastName

助手类(实际转换):

公共类JsonHelperClass{

//私有文件file=null;//创建临时文件
私营JSONNODEFARY工厂;
私人JsonFactory JsonFactory;
私有对象映射器映射器;
私有JsonNode-jsonRoot;
专用队列;
//专用JsonParser JsonParser=
public JsonHelperClass()抛出JsonProcessingException,IOException{
this.factory=JsonNodeFactory.instance;
this.jsonFactory=新的jsonFactory();
this.mapper=新的ObjectMapper();
this.jsonRoot=mapper.readTree(新文件(“带数据的json”);
}
公共静态void main(字符串[]args)引发异常,异常{
JsonHelperClass helperClass=新的JsonHelperClass();
helperClass.jsonCreator();
ObjectNode ObjectNode=null;
ObjectNode result=helperClass.CreateJSON递归(ObjectNode);
System.out.println(result.toString());
}
public void jsonCreator()引发异常{
Trie-Trie=TrieBuilder.createSpec();
queue=trie.bfsTraversal(trie.root);
}
公共ObjectNode CreateJSON递归(ObjectNode outputJson)引发异常{
三节点nodeOfQueue=queue.poll();
if(outputJson==null){
//创建JSON的根
outputJson=factory.objectNode();
put(nodeOfQueue.target,createjson递归地(outputJson));
}else if(jsonRoot.get(nodeOfQueue.source.isObject()){
//创建一个对象以包含其他值/对象
ObjectNode ObjectNode=factory.ObjectNode();
objectNode.put(nodeOfQueue.target,createjson递归(outputJson));
outputJson.putAll(objectNode);
}else if(jsonRoot.get(nodeOfQueue.source.isArray()){
//创建一个数组节点并调用以创建其包含的值
ArrayNode ArrayNode=factory.ArrayNode();
int size=jsonRoot.get(nodeOfQueue.source.size();
对于(int index=0;index
    source = com:track:trackDetails:fname, target = gsUser:gsProp:gsDetails:gsFirstName
    source = com:track:trackDetails:lname, target = gsUser:gsProp:gsDetails:gsLastName

public class JsonHelperClass{

//  private Files file = null;// create a tempfile
    private JsonNodeFactory factory;
    private JsonFactory jsonFactory;
    private ObjectMapper mapper;
    private JsonNode jsonRoot;
    private Queue<TrieNode> queue;
//  private JsonParser jsonParser =  

    public JsonHelperClass() throws JsonProcessingException, IOException {
        this.factory = JsonNodeFactory.instance;
        this.jsonFactory  = new JsonFactory();
        this.mapper  = new ObjectMapper();
        this.jsonRoot = mapper.readTree(new File("json with data"));
    }

    public static void main(String[] args) throws Exception, Exception {
            JsonHelperClass helperClass = new JsonHelperClass();
            helperClass.jsonCreator();
            ObjectNode objectNode = null; 
            ObjectNode result = helperClass.createJsonRecursively(objectNode);
            System.out.println(result.toString());

    }

    public void jsonCreator() throws Exception {
        Trie trie = TrieBuilder.createSpec();
        queue = trie.bfsTraversal(trie.root);

    }

    public ObjectNode createJsonRecursively(ObjectNode outputJson) throws Exception {
        TrieNode nodeOfQueue = queue.poll();

        if(outputJson == null){
            // create a root of the JSON
            outputJson = factory.objectNode();
            outputJson.put(nodeOfQueue.target, createJsonRecursively(outputJson));


        }else if (jsonRoot.get(nodeOfQueue.source).isObject()){
            // create an object to conatin other values/object
            ObjectNode objectNode = factory.objectNode();
            objectNode.put(nodeOfQueue.target,createJsonRecursively(outputJson));
            outputJson.putAll(objectNode);


        }else if(jsonRoot.get(nodeOfQueue.source).isArray()){
            // create an array node and call for to create value it contains
            ArrayNode arrayNode = factory.arrayNode();
            int size = jsonRoot.get(nodeOfQueue.source).size();
            for(int index = 0 ; index < size ; index++){
                arrayNode.add(jsonRoot.get(nodeOfQueue.source).get(index));
            }
            outputJson.put(nodeOfQueue.target,arrayNode);           
        }else if(nodeOfQueue.isEnd){
            // create leaf node
            outputJson.put(nodeOfQueue.target, jsonRoot.get(nodeOfQueue.source));
            return outputJson;

        }
        return outputJson;
    }