Java 只有最后一个值被添加到JSON对象
我有一个包含大量数据的xml文件。我已解析xml文件并尝试将值添加到JSON对象,但只添加了最后一个值。请在下面查找我的代码:Java 只有最后一个值被添加到JSON对象,java,json,xml-parsing,Java,Json,Xml Parsing,我有一个包含大量数据的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.
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();
}
}
预期结果:
它应该有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>
1
发射
0.203195
5084
1080112
7
0
24
卡基特
0.002845
576
1074752
6
0
24
卡基特
0.002845
576
1074752
6
0
我遗漏了什么吗?你在写:
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));
就像一个映射
,如果该值已经存在,则会覆盖该值。在每次迭代中,您只需覆盖JSON对象
您应该在每次迭代中创建一个新的JSONObject
,并将其存储在您正在编写的JSONArray
中:
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));
就像一个映射
,如果该值已经存在,则会覆盖该值。在每次迭代中,您只需覆盖JSON对象
您应该在每次迭代中创建一个新的
JSONObject
,并将其存储在JSONArray
中,您可以参考我的问题以更容易地实现
您可以很快使用JSON java库
可以使用以下代码将XML字符串转换为JSONObject
JSONObject data=XML.toJSONObject(xmlString)代码>
您可以在此处找到有关它的更多信息:
有了以上的参考资料,我至少能够实现这个解决方案。我希望这也能对其他人起作用
private static JSONObject extractData(NodeList nodeList, String tagName) throws TransformerConfigurationException,
TransformerException, TransformerFactoryConfigurationError, JSONException {
JSONObject resultObject = new JSONObject();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (!node.getNodeName().equals(tagName) && node.hasChildNodes()) {
return extractData(node.getChildNodes(), tagName);
} else if (node.getNodeName().equals(tagName)) {
DOMSource source = new DOMSource(node);
StringWriter stringResult = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(stringResult));
resultObject = XML.toJSONObject(stringResult.toString()).optJSONObject(tagName);
}
}
return resultObject;
}
public static JSONObject getFullData(String tagName, SOAPMessage message) throws Exception {
NodeList nodeList = message.getSOAPBody().getChildNodes();
JSONObject resultObject = extractData(nodeList, tagName);
return resultObject;
}
你可以参考我的问题,以便于实施
您可以很快使用JSON java库
可以使用以下代码将XML字符串转换为JSONObject
JSONObject data=XML.toJSONObject(xmlString)代码>
您可以在此处找到有关它的更多信息:
有了以上的参考资料,我至少能够实现这个解决方案。我希望这也能对其他人起作用
private static JSONObject extractData(NodeList nodeList, String tagName) throws TransformerConfigurationException,
TransformerException, TransformerFactoryConfigurationError, JSONException {
JSONObject resultObject = new JSONObject();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (!node.getNodeName().equals(tagName) && node.hasChildNodes()) {
return extractData(node.getChildNodes(), tagName);
} else if (node.getNodeName().equals(tagName)) {
DOMSource source = new DOMSource(node);
StringWriter stringResult = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(stringResult));
resultObject = XML.toJSONObject(stringResult.toString()).optJSONObject(tagName);
}
}
return resultObject;
}
public static JSONObject getFullData(String tagName, SOAPMessage message) throws Exception {
NodeList nodeList = message.getSOAPBody().getChildNodes();
JSONObject resultObject = extractData(nodeList, tagName);
return resultObject;
}
创建一个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);
JSONArray finalArray=new JSONArray();//创建jsonarray
对于(int i=0;i
创建一个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);
JSONArray finalArray=new JSONArray();//创建您的JSONArray
对于(int i=0;i
使用JSONObject
的JSONArray
在for循环中创建对象,并在对象填充后将对象添加到数组中使用JSONArray
的JSONObject
在for循环中创建对象,并在对象填充后将对象添加到数组中