gpt4 book ai didi

java - 仅最后一个值被添加到 JSON 对象

转载 作者:行者123 更新时间:2023-12-02 02:41:09 25 4
gpt4 key购买 nike

我有一个包含大量数据的 xml 文件。我已经解析了 xml 文件并尝试将值添加到 JSON 对象,但只添加了最后一个值。请在下面找到我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Process_Parser {

static JSONObject json= new JSONObject();
//static //JSONObject arrayvalue=new JSONArray();

public static void main(String args[]) {
try {

File process = new File("/Users/instrument.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(process);
doc.getDocumentElement().normalize();

//System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("process");
//System.out.println("==========================");

Element pageElement = (Element)doc.getElementsByTagName("process").item(0);
//NodeList result = pageElement.getElementsByTagName("processName");
System.out.println("Suba-----------"+nodes.getLength());

for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
//System.out.println(getValue("processName", element).contains("carkitd"));
//if(element.getElementsByTagName("process") != null){
if(element.getFirstChild().getNodeValue() != null){
if(getValue("processName", element).contains("carkitd")){


json.put("processName",getValue("processName", element));
json.put("cpuUsage",getValue("cpuUsage", element));
json.put("realmemory",getValue("realmemory", element));
json.put("Virtualmemory",getValue("Virtualmemory", element));
json.put("thread",getValue("thread", element));
json.put("cputime",getValue("cputime", element));

}
}

}
}
//System.out.println("result-array:" +arrayvalue);
System.out.println("result" +json);
//}
//System.out.println("arrayvalue::"+arrayvalue.size());
}catch (Exception ex) {
ex.printStackTrace();
}
}


private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}

}

当我运行上面的代码时,我得到以下结果

实际结果:

{

"Virtualmemory":"1100320.000000",
"cpuUsage":"0.000000",
"process":"carkitd",
"realmemory":"1044.000000",
"thread":"2.000000",
"cputime":"0.000000"
}

预期结果:

它应该有 67 个对象

示例 xml 文件:

<instrument>
<slice time ='1498215480919'>
<process>
<processId>1.000000</processId>
<processName>launchd</processName>
<cpuUsage>0.203195</cpuUsage>
<realmemory>5084.000000</realmemory>
<Virtualmemory>1080112.000000</Virtualmemory>
<thread>7.000000</thread>
<cputime>0.000000</cputime>
</process>
<process>
<processId>24.000000</processId>
<processName>carkitd</processName>
<cpuUsage>0.002845</cpuUsage>
<realmemory>576.000000</realmemory>
<Virtualmemory>1074752.000000</Virtualmemory>
<thread>6.000000</thread>
<cputime>0.000000</cputime>
</process>
<process>
<processId>24.000000</processId>
<processName>carkitd</processName>
<cpuUsage>0.002845</cpuUsage>
<realmemory>576.000000</realmemory>
<Virtualmemory>1074752.000000</Virtualmemory>
<thread>6.000000</thread>
<cputime>0.000000</cputime>
</process>
</slice>
</instrument>

我错过了什么吗?

最佳答案

创建一个 JSONArray 并继续在循环内插入 JSONObject

 JSONArray finalArray = new JSONArray(); // create your jsonarray
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
//System.out.println(getValue("processName", element).contains("carkitd"));
//if(element.getElementsByTagName("process") != null){
if (element.getFirstChild().getNodeValue() != null) {
if (getValue("processName", element).contains("carkitd")) {
JSONObject json = new JSONObject(); // your temp obj

json.put("processName", getValue("processName", element));
json.put("cpuUsage", getValue("cpuUsage", element));
json.put("realmemory", getValue("realmemory", element));
json.put("Virtualmemory", getValue("Virtualmemory", element));
json.put("thread", getValue("thread", element));
json.put("cputime", getValue("cputime", element));
finalArray.put(json); // push your values in the array
}
}

}
}
//System.out.println("result-array:" +arrayvalue);
System.out.println("result " + finalArray);

关于java - 仅最后一个值被添加到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45437349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com