[JAVA] Judgment of JSONArray and JSONObject

Judgment of JSONArray and JSONObject

Is the content of the JSON file an array or a simple substance? That

That's why I wrote it with a sudden idea like this. If the first character is [, it is judged to be JSONArray for the time being. Whether or not the string passes through the parser.

if(jsonStr.charAt(0) == '[') {
  //JSONArray
} else if(jsonStr.charAt(0) == '{') {
  //JSONObject
}

When I was looking for a smarter way, I found something like this

Object object = new JSONTokener(data).nextValue();
if (object instanceof JSONArray) {
  //JSONArray
} else if (object instanceof JSONObject) {
  //JSONObject
}

I think this is better

end

Recommended Posts

Judgment of JSONArray and JSONObject
[Java] Judgment of identity and equivalence
Equal value judgment of Integer and int
Judgment of the calendar
behavior of didSet and willSet
Overview of Docker and containers
Setup of JMeter and jEnv
Background and mechanism of Fabric-loader
Summary of FileInputStream and BufferedInputStream
Inclusion judgment of elements of Collection
Operator of remainder and exponentiation (exponentiation)
Advantages and disadvantages of Java
Judgment of fractions in Ruby
Basics of conditional branching and return
About fastqc of Biocontainers and Java
First launch judgment of Android application
Proper use of redirect_to and render
This and that of the JDK
[Swift] Advantages and disadvantages of Storyboard
[Apache POI] Judgment of unnecessary cells
Proper use of Mockito and PowerMock
[Rails] Differences and usage of each_with_index and each.with_index
About removeAll and retainAll of ArrayList
This and that of Core Graphics
Default implementation of Object.equals () and Object.hashCode ()
Application of downcase and slice methods
This and that of exclusive control
This and that of the implementation of date judgment within the period in Java