PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   27-07-2021

Json

! Json is the standard format for the communiation between web services. Even Json was derived from JavaScript, it is platform independent format. One of the main advantages, compared to XML, is the size of document.
 
import org.json.*;

public class App {

    public static void main(String[] args) {
        
        String s = "{message:{type:feed,timestamp:1},error:0}";
       
        JSONObject obj = new JSONObject(s);

        System.out.println(
                obj.getJSONObject("message").getString("type") // feed
        );
    }
}
... 4 lines
 

Types

! Json is a generic data format that has six data types (Strings, Numbers, Booleans, Arrays, Objects, null).
 
{
  "name": "Benjamin Watson",
  "age": 31,
  "isMarried": true,
  "hobbies": ["Football", "Swimming"],
  "kids": [
    {
      "name": "Billy",
      "age": 5
    }, 
   {
      "name": "Milly",
      "age": 3
    }
  ]
}
... 10 lines
 
 

import junit.framework.*;
import org.json.*;

public class App extends TestCase {

    public static void main(String[] args) {}

    public static void testJson() { // passed

        String str = "{message:{type:feed,timestamp:1},error:0}";
        JSONObject obj = new JSONObject(str);

        assertEquals("feed", obj.getJSONObject("message").getString("type"));
            // passed

        assertEquals(0, obj.getInt("error"));
            // passed
    }
}
... 7 lines
 
Translate    (2/2)

Translate

Custom formats can be easily transtated to Json.
 
import junit.framework.*;
import org.json.*;

public class App extends TestCase {

    
    public static void main(String[] args) {}

    public void testJson() { // passed

        String str = "{message={type=feed|timestamp=1},error=0}";
        JSONObject obj;

        str = translate(str);
        obj = new JSONObject(str);

        assertEquals("feed", obj.getJSONObject("message").getString("type"));
            // passed

        assertEquals(0, obj.getInt("error"));
            // passed
    }
    
    public 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;
    }
}
... 16 lines
 

Loop

To parse json array, use for-each loop with getJSONArray() method.
 
import junit.framework.*;
import org.json.*;

public class App extends TestCase {

    public static void main(String[] args) {}

    public static void testJson() { // passed

        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(int i=0; i<orders.length(); i++) { // for

            JSONObject order = orders.getJSONObject(i); // Look Here
            assertTrue(order.getString("sym").length() == 4);
            assertTrue(order.getInt("amount") > 0);
        }

        for(Object item : orders) { // for-each

            JSONObject order = (JSONObject) item; // Look Here
            assertTrue(order.getInt("error") >= 0);
            assertTrue(order.getInt("error") <= 1);
        }
    }
}
... 20 lines
 

Questions     github Collections
Create