Java vs Dart: Decoding JSON
Hi Folks! Today I will be talking about one of the feature of Dart which really shines out compared to Java. The feature we will be discussing in this article is about decoding JSON.
If you are an Android developer like me then you must have worked with JSON by now. If not than don’t sweat it out. I will give you a very simple definition of JSON.
What is JSON?
JSON is a format which is used for transmitting structured data over a network connection.
This is how a JSON structure looks like:
{
"albumId": 1,
"id": 1,
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
}
As this article is mostly about decoding JSON in Java and Dart. I will expect you to read this stackoverflow answer to have a better understanding of what JSON is all about.
Now lets get back to our main topic i.e What is it like decoding JSON in Java and doing the same in Dart. Which one is easy? Which one has less line of coding? Which one is simple or confusing? Lets find out:
Lets take this JSON structure and try to decode the same in both Java and Dart. Finally we will see which one seems easy and friendly.
[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "http://placehold.it/600/771796",
"thumbnailUrl": "http://placehold.it/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "http://placehold.it/600/24f355",
"thumbnailUrl": "http://placehold.it/150/24f355"
}
]
Decoding the above JSON structure in Java:
public static void main(String[] args) {
String json = "[\n" +
" {\n" +
" \"albumId\": 1,\n" +
" \"id\": 1,\n" +
" \"title\": \"accusamus beatae ad facilis cum similique qui sunt\",\n" +
" \"url\": \"http://placehold.it/600/92c952\",\n" +
" \"thumbnailUrl\": \"http://placehold.it/150/92c952\"\n" +
" },\n" +
" {\n" +
" \"albumId\": 1,\n" +
" \"id\": 2,\n" +
" \"title\": \"reprehenderit est deserunt velit ipsam\",\n" +
" \"url\": \"http://placehold.it/600/771796\",\n" +
" \"thumbnailUrl\": \"http://placehold.it/150/771796\"\n" +
" },\n" +
" {\n" +
" \"albumId\": 1,\n" +
" \"id\": 3,\n" +
" \"title\": \"officia porro iure quia iusto qui ipsa ut modi\",\n" +
" \"url\": \"http://placehold.it/600/24f355\",\n" +
" \"thumbnailUrl\": \"http://placehold.it/150/24f355\"\n" +
" }\n" +
"]";
//Printing the JSON objects in array JSONArray array = new JSONArray(json);
for(int i=0;i<array.length();i++){
System.out.println(array.get(i).toString());
}
//Printing a value in a JSON object JSONObject jsonObject = array.getJSONObject(0);
System.out.print(jsonObject.getString("title"));
}
In the above code I took the JSON string and converted it to JSONArray object. I printed all the JSON objects living inside the JSONArray object and a value from the first JSONObject in the array. If you are seeing JSONArray and JSONObject in red colour. Please download and import this library in your code editor to make it work.
Now I will do the same in Dart. Check the below code:
import'dart:convert';
void main(){
var str = '[{"albumId": 1,"id": 1,"title": "accusamus beatae ad facilis cum similique qui sunt","url": "[http://placehold.it/600/92c952](http://placehold.it/600/92c952)","thumbnailUrl": "[http://placehold.it/150/92c952](http://placehold.it/150/92c952)"},{"albumId": 1,"id": 2,"title": "reprehenderit est deserunt velit ipsam","url": "[http://placehold.it/600/771796](http://placehold.it/600/771796)","thumbnailUrl": "[http://placehold.it/150/771796](http://placehold.it/150/771796)"},{"albumId": 1,"id": 3,"title": "officia porro iure quia iusto qui ipsa ut modi","url": "[http://placehold.it/600/24f355](http://placehold.it/600/24f355)","thumbnailUrl": "[http://placehold.it/150/24f355](http://placehold.it/150/24f355)"}]';
//Printing the JSON objects in array
var jsonholder=json.decode(str);
for(var obj in jsonholder){
print(obj);
}
//Printing a value in a JSON object
print(jsonholder[0]['id']);
}
Dart has a very strong set of in-built libraries that we can use it for our advantages. Here I am using the json library from the dart:convert package. But for Java we have to get a third party library to parse JSON. Which I feel is a little disadvantage here.
I would like to conclude here by saying that Dart is really amazing and a great choice for mobile app development(Flutter). It will make development really easy and I strongly believe that Flutter is the future for mobile app development which uses Dart as the primary programming language.
I hope you liked this simple article. If you have any doubt or confusion feel free to connect with me at LinkedIn or comment your valuable thoughts below. Will be back soon with a new article.