JSON-java icon indicating copy to clipboard operation
JSON-java copied to clipboard

XML to json Array Issue

Open smtrad opened this issue 10 years ago • 8 comments

I need to parse a specific XML element of xml content, that has only one occurency, to a JSON Array object, using XML.toJSONObject(xml). How do this?

When the XML element appears more than one occurence, it's work fine!

smtrad avatar Jun 02 '16 11:06 smtrad

since this project does not support document specifications, there is no way to know that it is supposed to be an array until more than 1 of the same element shows up in the document. You can't do it using this project. The way to handle it would be something like this:

JSONObject jo = XML.toJSONObject(myString);
if(jo.optArray("myKey") != null){
  // do array work
  for(Object o : jo.getArray("myKey")){
  // do something with each value in the array
  }
} else if (jo.opt("myKey")!=null) {
 // not an array
 Object o = jo.opt("myKey");
 // do something with this value.
}

johnjaylward avatar Jun 13 '16 19:06 johnjaylward

Hello John, Can you help me where should I add this code?

narendar225 avatar Jul 25 '16 20:07 narendar225

I wouldn't know a specific use for it without seeing your code that needs it.

johnjaylward avatar Jul 25 '16 20:07 johnjaylward

I have to convert XML Elements (maxoccurs='unbound') with single occurrence into JSON arrays.

Ex:

<Books><title>Games<title></Books>   (Here 'title' is maxoccurs)

result (I am getting from JSON-java)
{
"Books" : 
  { 
   "title":"Games"
  }
}

`But` I am expecting
{
"Books" : 
  { 
   "title":[ "Games"]
  }
}

I have downloaded all the classes from JSON-java.In which java class should I add the code you mentioned? really appreciate your help!!

narendar225 avatar Jul 25 '16 21:07 narendar225

String xml = "<Books><title>Games<title></Books>";

JSONObject jo = XML.toJSONObject(xml);

JSONObject books = jo.getJSONObject("books");

if(books.optArray("title") != null){
   JSONArray titles = books.optArray("title");
  // do something with our multiple titles
} else if(books.optString("title") != null) {
  String title = books.getString("title");
  // do something with the single title
} else {
  // no title.. may not be an error, but handle it here.
}

johnjaylward avatar Jul 25 '16 22:07 johnjaylward

Hello John, Sorry I am new to Java. I still don't understand where can I add that code. Do I have use it in any of JSON-Java classes ? or Do I have to create separate class and use this code there?

narendar225 avatar Jul 26 '16 19:07 narendar225

It would be in your own class that is processing the XML file. I don't know what you are trying to do with the XML, so I'm sorry I can't give you a more precise answer.

Your expected case that you show won't happen unless you have multiple titles:

<Books><title>Games<title><title>Self Help<title></Books>

then you would see this:

{
"Books" : 
  { 
   "title":["Games","Self Help"]
  }
}

that's why you need to check to see if the title property on the JSON object is an array or just a string

johnjaylward avatar Jul 26 '16 19:07 johnjaylward

Hi John, But it not working for single element, its works for multiple</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/25960327?v=4" alt="Narendra2github avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 18 '20 11:05 </span> <a class="owner-name" href="http://gitmemories.com/Narendra2github"> Narendra2github </a> </div> </div> <div class="clear-both"></div> </div> </section> </div> </div> </div> <aside class="detail-info-box"> <h3 class="title pkg-infobox-metadata">Labels</h3> <script type="text/javascript"> atOptions = { 'key' : '6ca19c2b16e3f20a28885886ef99a68e', 'format' : 'iframe', 'height' : 250, 'width' : 300, 'params' : {} }; </script> <script type="text/javascript" src="https://www.highperformanceformat.com/6ca19c2b16e3f20a28885886ef99a68e/invoke.js" ></script> <h3 class="title">Owner</h3> <div class="owner-info flex-items-center d-flex"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/19696926?u=dd60d58ea5ebb2ea74ac2df01d348ef0c74f426f&v=4" alt="smtrad avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <a class="owner-name" href="http://gitmemories.com/smtrad"> smtrad </a> </div> <hr class="color1"> <h3>Other Repo Issues</h3> <div class="packages"> </div> <div style="clear: both"></div> </aside> </div> </div> </main> <footer class="site-footer"> <span>© 2022 Git Memory </span> <a class="link sep" href="http://gitmemories.com/privacy-policy">Policy</a> <a class="link sep" href="http://gitmemories.com/terms">Terms </a> <a class="link sep" href="http://gitmemories.com/contact">Contact </a> <a class="link sep" href="https://exchangetuts.com">Exchangetuts </a> <a class="link sep" href="https://onltools.com/">Onltools </a> <a class="link sep" href="https://serveraide.com/">Serveraide </a> </footer> <script src="http://gitmemories.com/js/app.js?id=a1337f444c840fae34ad"></script> </body> </html>