如何用java递归生成带children的json串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用java递归生成带children的json串相关的知识,希望对你有一定的参考价值。

参考技术A 先递归出一个
node对象,再将node对象转换成json串。直接进行字符串的json操作不建议。
class
Node
private
String
id;
private
String
parentId;
private
List<Node>
children;
public
void
addChildren(Node
child)
getChildren().add(child);

public
List<Node>
getChildren();
public
String
transformTojson(Node
node
,StringBuffer
jsonStr)
//递归
if(jsonStr
==null)
StringBuffer
jsonStr=new
();

if(node.id!=null)
//拼接父节点json串
jsonStr.append(id:node.id,children:#&children);

List
childNodeList
=
node.getChildren();
StringBuffer
childrenSB
=
new();
for(
Node
node
:childNodeList)
childrenSB=transformTojson(node);

sonStr.replaceFirst("#&children",childrenSB);
return
jsonStr;


伪代码大概这样。
transformTojson方法可以不自己写,直接使用第三方工具jar包的json转化方法,比如gjson。

如何用java程序读取带密码(密码已知)的office文档。

如何用java程序 读取带密码(密码已知)的office文档。我用输入输出流以及poi接口打开读取了不带密码的office文档,但是带密码的不知道如何打开读取,跪求高手。邮箱bentengwu@163.com
对不起,请看清楚题目在回答!读取带密码的,密码已知的office文档。我要的只是一个方法,可以用程序输入密码而不影响到输入输出流。

参考技术A OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。水平有限,万望各位指正
WORD:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException
String bodyText = null;
try
WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream
bodyText = ex.getText();
if(!bodyText.equals(""))
index.AddIndex(url, title, bodyText);

catch (DocCenterException e)
throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
catch(Exception e)
e.printStackTrace();


return null;

Excel:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException
StringBuffer content = new StringBuffer();
try
HSSFWorkbook workbook = new HSSFWorkbook(is);//创建对Excel工作簿文件的引用
for (int numSheets = 0; numSheets

以上是关于如何用java递归生成带children的json串的主要内容,如果未能解决你的问题,请参考以下文章

如何用java递归生成带children的json串啊?数据集已经有了在下面。

如何用Java拼接JSON方式遍历整个树形节点

如何用java程序读取带密码(密码已知)的office文档。

java构建树形列表(带children属性)

如何用java生成有规律的字符串?

如何用Python解析多层嵌套的JSON?