aspose file tools*
The moose likes Other Open Source Projects and the fly likes XML to JSONObject, bug? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "XML to JSONObject, bug?" Watch "XML to JSONObject, bug?" New topic
Author

XML to JSONObject, bug?

eric wong
Greenhorn

Joined: Aug 19, 2011
Posts: 4
I am looking some xml to json sample for my application, i found the great example on the website, however i got some problem/bug i can't fix it

the result of xml to json become like this:
{"root":{"d":0.345,"e":155826477,"b":"bbb","c":123,"a":"AAA","postCode":28}}

1) i am no idea what is the meaning of 155826477 it should be 001122334455
2) 000.345 become to 0.345, and 028 become to 28
3) is it possible to remove the double quote within the json string?



================================================================================

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

public class Test {
public static void main(String[] args) {
try {
String xmlString = "<root><a>AAA</a><b>bbb</b><c>123</c><d>000.345</d><e>001122334455</e><postCode>028</postCode></root>";
JSONObject jsonObj = XML.toJSONObject(xmlString);
System.out.println(jsonObj.toString());
} catch (JSONException e) {
System.out.println(e.getMessage());
}
}
}
================================================================================
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Looks like it's treating integers which start with zero as being octal numbers. This is standard for Javascript literals and apparently your code follows the same rule.

And it looks like it treats things which are apparently numeric as being numbers, so dropping leading zeroes would be an acceptable thing to do in Javascript.

And it looks like it surrounds text data with double quotes, which is how Javascript represents text. You didn't say why you objected to that.

So in summary you don't like the way this code converts your XML to JSON, although it's following the Javascript rules. There may be options you can supply to the code to make it work differently; you'd have to check its documentation for that.

Edit: Also, remember that the code you have is meant to represent arbitrary JSON data in XML, not to represent arbitrary XML data in JSON. So the test you should apply is whether it converts JSON data to XML accurately.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

eric wong wrote:1) i am no idea what is the meaning of 155826477 it should be 001122334455

Why do you care what radix the value is expressed in as long as the value is correct?

2) 000.345 become to 0.345, and 028 become to 28

As Paul pointed out, leading zeros would be incorrect.

3) is it possible to remove the double quote within the json string?

Of course not. If it were, it would not be valid JSON!

These are not bugs -- they are valid JSON.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
eric wong
Greenhorn

Joined: Aug 19, 2011
Posts: 4
Thank you for all kindly reply ^^

Let me make some example on leading zero

1) some time double value should be considered to be a double
e.g. <exchangeRate>000.023<exchangeRate>
become json = 0.023, excepted and acceptable value for this case

2) some time double value should be considered to be a string
e.g. <internationalPhoneNumber>009988776655</internationalPhoneNumber>
become json = 9988776655, incorrect value

e.g. <internationalPhoneNumber>001122334455</internationalPhoneNumber>
become json = 155826477, bug =.=?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That means that if you want to produce XML which converts to JSON according to your rules, you're going to have to represent the data in your XML according to Javascript's rules.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Ah, yes. I understand the string conversion problem. I haven't used that particular library, so I can't give you a specific solution, but most JSON converters will have a means to control the conversion mapping. I'd dig into the docs to find out how to do it with your library.
eric wong
Greenhorn

Joined: Aug 19, 2011
Posts: 4
Thank you for all comments~

 
Consider Paul's rocket mass heater.
 
subject: XML to JSONObject, bug?