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.
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 ~~
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.
About Android Json file acquisition https://nextat.co.jp/staff/archives/5
Recommended Posts