Android development, how to check null in the value of JSON object

Decoding JSON objects from APIs in Android development

Today, I was addicted to parsing JSON with the implementation related to API, so I will organize my knowledge. It's an easy win on iOS, but it was a little different on Android. I got the impression that Android is easier to see because it has a shallower nest. For iOS, API communication is implemented with Alamofire or APIKit, so the closure is written. To go. If you are good at subdividing methods, the nesting will not be too deep and it will not be ugly, Up until now, there was a 2/3 chance that the nest would be deeper as if it had been assigned to various sites.

However, Android doesn't have a culture of closures, so it doesn't seem to need much nesting. However, since there is a lambda notation, there is a flow of (argument)-> (processing content), so if you burst lambda Perhaps deep nesting may occur.

Now, suppose you have a Json file like the one below.

sample.json


[
  {
    "access":"15 minutes walk from Hikone Station on the JR Biwako Line",
    "address":"1 Konkicho, Hikone City, Shiga Prefecture-1",
    "name":"Hikone Castle",
    "service":"Admission: Adults 600 yen, Children 200 yen Set ticket with museum 1000 yen for adults, 350 yen for children",
    "tel":"0749-22-2742 (Hikone Castle Management Office)",
    "url":"http://www.hikoneshi.com/",
  "pref" : null
  },
  {
    "access":"25 minutes walk from Azuchi Station on the JR Biwako Line",
    "address":"Shimotoyoura, Azuchi-cho, Omihachiman City, Shiga Prefecture",
    "name":"Azuchi Castle Ruins",
    "service":"Entrance fee: Adults 500 yen, Children 100 yen Admission fee (Saturday and Sunday) 500 yen",
    "tel":"0748-46-4234",
    "url":"http://www.azuchi-shiga.com/",
  "pref" : null¥
  }
]

This is borrowed from the reference site below. For those who want to arrange JSON https://lab.syncer.jp/Tool/JSON-Viewer/ Please do your best with such things.

Parsing JSON objects on Android

If you want to parse on Android, use JSONArray to create a JSON array object.

readjson.java


JSONArray jsons = new JSONArray(JsonFile);
for (int i = 0; i < jsons.length(); i++){
  JSONObject jsonRslt = jsons.getJSONObject(i);
  String access = jsonRslt.getString("access");
  String address = jsonRslt.getString("address");
  String name = jsonRslt.getString("name");
  String service = jsonRslt.getString("service");
  String tel = jsonRslt.getString("tel");
  String url = jsonRslt.getString("url");
}

You can now store objects in each variable. The method name is more conscientious than Objective-C on iOS. You can't make a mistake. ~~ Compared to that, when it comes to Objective-C ~~

About null check on Android

I didn't know the null check for the json key in Android development, so I looked it up. Android seemed to crash the app when accessing null. (Of course)

The method seems to be as follows. If you have any other best practices, please let me know. I rarely used it in iOS development, but it is also a point to throw an error with try-catch syntax.

json.java



JSONObject object = jsons //Create variable for json object
if (jsonObject.isNull("pref")) {
    //value for pref(value)Is null This time it passes
} else {
    //value for pref(value)Is not null
}

When I try this Am I the only one who thinks iOS development is more difficult than anything else? When I searched for iOS, I only hit English pages, so I had to use unreliable code, but You can quickly find best practices on Android. ~~ Java engineer Is it really okay? Is this easy? ~~

With iOS, I used to search by relying on Stack Over Flow. No matter how you think about it, Java engineers can't help but feel comfortable.

Reference page

About Android Json file acquisition https://nextat.co.jp/staff/archives/5

Recommended Posts

Android development, how to check null in the value of JSON object
How to implement one-line display of TextView in Android development
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to increment the value of Map in one line in Java
How to check the logs in the Docker container
How to change the value of a variable at a breakpoint in intelliJ
How to check Rails commands in the terminal
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
How is the next value of the Time object correct?
How to check the WEB application created in the PC development environment on your smartphone
How to correctly check the local HTML file in the browser
How to use the getter / setter method (in object orientation)
How to retrieve the hash value in an array in Ruby
How to change the setting value of Springboot Hikari CP
Mapping to a class with a value object in How to MyBatis
How to derive the last day of the month in Java
How to check the database of apps deployed on Heroku
About the basics of Android development
[Rails] Check the contents of the object
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to display 0 on the left side of the standard input value
How to output the value when there is an array in the array
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to check for the contents of a java fixed-length string
I want to change the value of Attribute in Selenium of Ruby
How to get the length of an audio file in java
Method definition location Summary of how to check When defined in the project and Rails / Gem
How to determine the number of parallels
How to sort the List of SelectItem
How to pass the value to another screen
How to get the date in java
How to assemble JSON directly in Jackson
How to use ExpandableListView in Android Studio
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
How to output the sum of any three numbers, excluding the same value
I tried to make a parent class of a value object in Ruby
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
Procedure to make the value of the property file visible in Spring Boot
How to execute with commands of normal development language in Docker development environment
How to change the maximum and maximum number of POST data in Spark
How to find the total number of pages when paging in Java
How to constrain the action of the transition destination when not logged in
How to get the setting value (property value) from the database in Spring Framework
How to get the absolute path of a directory running in Java
How to create your own annotation in Java and get the value
[Android] Develop a service that allows university students to check the operating status of buses circulating in the university.
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
[Swift] How to get the number of elements in an array (super basic)
Summary of how to select elements in Selenium
How to find the cause of the Ruby error
graphql-ruby: How to get the name of query or mutation in controller Note
Customize how to divide the contents of Recyclerview
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
How to add sound in the app (swift)
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
How to get today's day of the week
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Output of how to use the slice method
How to store an object in PostgreSQL as JSON with MyBatis (Mapper XML)