Java
/
Collections
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Json
Compare to XML, JSON is much smaller in size.T
package com.minte9.collections.json;
import static org.junit.Assert.assertEquals;
import org.json.JSONObject;
import org.junit.Test;
public class JsonTest {
@Test public void json() {
String s = "{message:{type:feed,timestamp:1},error:0}";
JSONObject obj = new JSONObject(s);
assertEquals("feed", obj.getJSONObject("message").getString("type"));
// passed
assertEquals(0, obj.getInt("error"));
// passed
}
}
Translate
Custom formats can be easily transtated to Json.
/**
* Translate custom formats to json for easy parsing.
*/
package com.minte9.collections.json;
import org.json.JSONObject;
public class Translate {
public static void main(String[] args) {
//String s = "{message:{type:feed,timestamp:1},error:0}"; // json
String s = "{message={type=feed|timestamp=1},error=0}"; // not a json
JSONObject obj;
s = translate(s);
obj = new JSONObject(s);
System.out.println(
obj.getJSONObject("message").getString("type") // feed
);
System.out.println(
obj.getInt("error") // 0
);
}
public static String translate(String s) {
s = s.replaceAll("([^={}|]+)=", "$1="); // add quotes to keys
s = s.replaceAll("=([^={}|]+)", "=$1"); // add quotes to values
s = s.replace("|", ",").replace("=", ":");
// System.out.println(s);
return s;
}
}
Loop
To parse json array, use for-each loop with getJSONArray() method.
/**
* Use getJSONArray() in order to loop an json
*/
package com.minte9.collections.json;
import org.json.JSONArray;
import org.json.JSONObject;
public class Loop {
public static void main(String[] args) {
String str = "{"
+ "orders: ["
+ "{sym:goog,amount:100,error:1},"
+ "{sym:msft,amount:200,error:0}"
+ "], error:0" +
"}";
JSONObject obj = new JSONObject(str);
JSONArray orders = obj.getJSONArray("orders");
//System.out.println(orders);
for(Object item : orders) {
JSONObject order = (JSONObject) item; // Look Here
System.out.println(""
+ order.getString("sym")
+ ":"
+ order.getInt("error")
);
// goog:1
// msft:0
}
}
}
➥ Questions