嵌套JSON的文本

时间:2015-06-03 07:52:30

标签: java json

我们有来自猪存储在文本文件中的输出。它们是包含url形式的用户路径的元组。

以下是2个样本元组:

    (http://www.flipkart.com/",http://www.flipkart.com/jewellery",http://www.flipkart.com/jewellery/rings",http://www.flipkart.com/jewellery/rings/Agni"):1
(http://www.flipkart.com/",http://www.flipkart.com/jewellery",http://www.flipkart.com/jewellery/rings",http://www.flipkart.com/jewellery/rings/Nakshatra"):1

我们希望我们的输出以嵌套的json形式出现:

{"id":0,      
"name":"(http:\/\/www.flipkart.com\/\"",
"data":"",
"children":
{"id":1,
"name":"http:\/\/www.flipkart.com\/jewellery\"",
"data":"",
"children":
    {"id":2,
    "name":"http:\/\/www.flipkart.com\/jewellery\/rings\"",
    "data":"","children":
        [{"id":3,
        "name":"http:\/\/www.flipkart.com\/jewellery\/rings\/Agni\"):1",
        "data":"",
        "children":"[]"
        },
        {"id":4,
        "name":"http:\/\/www.flipkart.com\/jewellery\/rings\/Nakshatra\"):1",
        "data":"",
        "children":"[]"
        }]
    }
}
}

我们怎样才能在java中做到这一点?

1 个答案:

答案 0 :(得分:0)

没有捷径 - 你需要清理字符串并制作Json。我想你是想做一个jstree?

如果你想用Javascript做 - 写一个正则表达式,运行一个循环来添加其他变量并转换JSON.stringify方法转换为Json对象。或者在java清理字符串的情况下,使用GSON或任何其他库生成pojo并创建json对象。