I have used XML to JSON converter from json.org. My problem is that the Boolean & integers are shown without double quotes in the converted JSON.
My JSON has to be sent to UI which uses Javascript for Cordova app for iOS & Android app. Since conversion in iOS retains the double quotes when converting from XML to JSON, in Android also I have to retain them. In iOS there is no way remove the double quotes during conversion.
e.g. XML snippet
<handle>
<price>20</price>
<item>chair</item>
<tax>true</tax>
</handle>
XML to JSON converter from json.org converts to
"handle": { "tax": true, "item": "chair", "price": 20 }
Expected to converted to following JSON
"handle": { "tax": "true", "item": "chair", "price": "20" }
Please let me know if there any other converting libraries/options using which this can be achieved?
Thanks in advance.
According to this:
https://github.com/douglascrockford/JSON-java/blob/master/XML.java#L303
No. At least not the 'easy' way. Either use a different library or use this to build the JSON you want, but do your XML parsing manually and use appendString when building the JSON.
That said, I'm not sure this is really what you want. You should reconsider why it is you need the "true" in string form, it seems highly likely to me that the real bug is somewhere else and this might be a way to avoid having to fix the real issue. Not judging you, I've been there before.
Related
I have thousand of strings in Json and I wanted to convert into xml(strings.xml) .Is there any short way to do that ? plugins ? or I have to do it one by one?
If you want to convert the json to xml in your app, you can use any existing library. This answer refers to one such.
If you just want to simply convert a json contents to xml, you can use any online converter. This one came first in my google search result. (But I would check the site's reliability before uploading any sensitive data)
You can read the entire JSON into a String object, and get the XML representation with Regex and .replace() method Of a String
Here is a smaple
val json = """{
"text_download_complete":"your file is downloaded"
}"""
val xml = json.replace("\\{\\s*".toRegex(), "<string name=")
.replace(":\\s*\"".toRegex(), ">")
.replace("\"\\s*\\}".toRegex(), "</string>")
Log.d("LOG_TAG", "onCreate: XML:\n$xml")
You can also read the JSON file instead of coping the entire file into a String...
I believe that won't be the optimum solution, but I think it would be a one shot task, and just wanted to be simple rather than looking at performance.
I'm no back-end developer. So perspective is always appreciated.
I have written a script which requests from an API and creates this huge JSON file I want to save in firebase, how can I accomplish this? And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region or do I absolutely need to request the entire json file and parse in my code (java android) ?
Since there are a few parts to your question:
You can save JSON to Firebase and the data will be mapped to child locations:
Using PUT, we can write a string, number, boolean, array or any JSON object to our Firebase database...When a JSON object is saved to the database, the object properties are automatically mapped to child locations in a nested fashion.
https://firebase.google.com/docs/database/rest/save-data
And for your next question:
And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region
Looks like you should be able to jimmy something together with Firebase's filters, startAt and endAt:
We can combine startAt and endAt to limit both ends of our query.
https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering
For your example you might do something like this:
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&startAt="EU"&endAt="EU"&print=pretty'
...or do I absolutely need to request the entire json file and parse in my code (java android) ?
The facts that JSON objects are stored hierarchically in Firebase and that you can filter based on those object values makes me think you do not, in fact, have to request the entire JSON file. However, I don't have personal experience with this particular aspect of Firebase, so give it a shot!
As #ackushiw mentions in the comments, you can also use the equalTo query (https://firebase.google.com/docs/reference/js/firebase.database.Query#equalTo):
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&equalTo="EU"&print=pretty'
It really depends on how you are structuring your JSON. It's generally recommended to make your JSON tree as shallow as possible since all children are loaded when you have a matching query.
FIREBASE DATA:
{
"-id1": {
"region": "eu" // bear in mind queries are case sensitive
"title": "Foo"
"nested": {
"city": "berlin"
}
},
"-id2": {
"region": "other"
"title": "Bar"
"nested": {
"city": "berlin"
}
},
"-id3": {
"region": "eu"
"title": "Baz"
"nested": {
"city": "paris"
}
}
}
Querying with (using the Android API)
.orderByChild("region").equalTo("eu")
would return "-id1" and "-id3"
with
.orderByChild("nested/city").equalTo("berlin")
would return "-id1" and "-id2"
The REST API Returns Unsorted Results: JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
If you're using a more complex structure I recommend watching this video
https://www.youtube.com/watch?v=vKqXSZLLnHA
I'd also recommend using the firebase library for Android
https://firebase.google.com/docs/android/setup
And Firebase-UI, It does a lot for you.
https://firebaseopensource.com/projects/firebase/firebaseui-android/
I am developing an e-commerce android application. am stuck in a situation, i don't know how to handle the situation. That is,
How can i post dynamic json values in Retrofit. For example
{
"type":"1",
"typeId":"10",
"userId":"15",
"filters":{
"price":"from-to",
"attribute_code":"values",
"...":"....",
"...":"...."
}
}
in this the "filter" is another object in the main json. the values in "filter" json is dynamic like sometime it may have "price", "size" or sometimes it may have "price" only or some times it may have "price","size","offers" etc..
How can we handle this circumstance..
Thanks
I am creating a question answer kind of app in android and passing data through json file but i need how to pass a code or syntax in json file which display properly in my android app.
"Question": "what is json?",
"Answer": "Javascript Object Notation",
"Image": "json.png"
//code or syntax
But how to pass a code here?
I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.
{
"30_year_rate": 4.25,
"30_year_pi": 196.78,
"30_year_apr": 4.375,
"20_year_rate": 4.375,
"20_year_pi": 250.37,
"20_year_apr": 4.5,
"15_year_rate": 3.75,
"15_year_pi": 290.89,
"15_year_apr": 3.875,
"10_year_rate": 3.625,
"10_year_pi": 397.89,
"10_year_apr": 3.75,
"5_year_arm": 2.75,
"5_year_pi": 163.3,
"5_year_apr": 2.875,
"7_year_arm": 3,
"7_year_pi": 168.64,
"7_year_apr": 3.125,
"3_year_arm": 4,
"3_year_pi": 190.97,
"3_year_apr": 4.125,
"5_year_io_arm": "N/A",
"5_year_io_pi": "N/A",
"5_year_io_apr": "N/A",
"QuoteID": 1449,
"Licensed": "N"
}
How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.
If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.
JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");
etc.
You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.
Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.
To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.
To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data