ÏÂÃæÊDZàÂë³ÉJSON´®µÄÒ»¸ö¼òµ¥µÄÀý×Ó¡£
import org.json.simple.JSONObject; import org.json.simple.JSONArray; public class JsonEncodeDemo { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("Novel Name", "Godaan"); obj.put("Author", "Munshi Premchand"); JSONArray novelDetails = new JSONArray(); novelDetails.add("Language: Hindi"); novelDetails.add("Year of Publication: 1936"); novelDetails.add("Publisher: Lokmanya Press"); obj.put("Novel Details", novelDetails); System.out.print(obj); } }Êä³ö£º
{"Novel Name":"Godaan","Novel Details":["Language: Hindi","Year of Publication: 1936","Publisher: Lokmanya Press"],"Author":"Munshi Premchand"}JSON½âÎö¿ª·¢ÈËÔ±ÒªÏë½âÎöJSON´®£¬Ê×ÏÈÄãµÃÖªµÀËüµÄ¸ñʽ¡£ÏÂÃæÀý×ÓÓÐÖúÓÚÄãÀ´Àí½âÕâÒ»µã£º
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JsonParseTest { private static final String filePath = "//home//user//Documents//jsonDemoFile.json"; public static void main(String[] args) { try { // read the json file FileReader reader = new FileReader(filePath); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject)jsonParser.parse(reader); // get a number from the JSON object Long id = (Long) jsonObject.get("id"); System.out.println("The id is: " + id); // get a String from the JSON object String type = (String) jsonObject.get("type"); System.out.println("The type is: " + type); // get a String from the JSON object String name = (String) jsonObject.get("name"); System.out.println("The name is: " + name); // get a number from the JSON object Double ppu = (Double) jsonObject.get("ppu"); System.out.println("The PPU is: " + ppu); // get an array from the JSON object System.out.println("Batters:"); JSONArray batterArray= (JSONArray) jsonObject.get("batters"); Iterator i = batterArray.iterator(); // take each value from the json array separately while (i.hasNext()) { JSONObject innerObj = (JSONObject) i.next(); System.out.println("ID "+ innerObj.get("id") + " type " + innerObj.get("type")); } // get an array from the JSON object System.out.println("Topping:"); JSONArray toppingArray= (JSONArray) jsonObject.get("topping"); Iterator j = toppingArray.iterator(); // take each value from the json array separately while (j.hasNext()) { JSONObject innerObj = (JSONObject) j.next(); System.out.println("ID "+ innerObj.get("id") + " type " + innerObj.get("type")); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (ParseException ex) { ex.printStackTrace(); } catch (NullPointerException ex) { ex.printStackTrace(); } } }jsonDemoFile.json
{ "id": 0001, "type": "donut", "name": "Cake", "ppu": 0.55, "batters": [ { "id": 1001, "type": "Regular" }, { "id": 1002, "type": "Chocolate" }, { "id": 1003, "type": "Blueberry" }, { "id": 1004, "type": "Devil's Food" } ], "topping": [ { "id": 5001, "type": "None" }, { "id": 5002, "type": "Glazed" }, { "id": 5005, "type": "Sugar" }, { "id": 5007, "type": "Powdered Sugar" }, { "id": 5006, "type": "Chocolate with Sprinkles" }, { "id": 5003, "type": "Chocolate" }, { "id": 5004, "type": "Maple" } ] }The id is: 1 The type is: donut The name is: Cake The PPU is: 0.55 Batters: ID 1001 type Regular ID 1002 type Chocolate ID 1003 type Blueberry ID 1004 type Devil's Food Topping: ID 5001 type None ID 5002 type Glazed ID 5005 type Sugar ID 5007 type Powdered Sugar ID 5006 type Chocolate with Sprinkles ID 5003 type Chocolate ID 5004 type Maple¼òµ¥×Ö·û´®²éÕÒJavaÌṩÁËÒ»¸ö¿âº¯Êý½Ð×öindexOf()¡£Õâ¸ö·½·¨¿ÉÒÔÓÃÔÚString¶ÔÏóÉÏ£¬Ëü·µ»ØµÄÊÇÒª²éÕÒµÄ×Ö·û´®ËùÔÚµÄλÖÃÐòºÅ¡£Èç¹û²éÕÒ²»µ½Ôò»á·µ»Ø-1¡£
ÁгöĿ¼ÏµÄÎļþÄã¿ÉÒÔÓÃÏÂÃæµÄ´úÂëÀ´ÁгöĿ¼ÏµÄÎļþ¡£Õâ¸ö³ÌÐò»á±éÀúij¸öĿ¼ÏµÄËùÓÐ×ÓĿ¼¼°Îļþ£¬²¢´æ´¢µ½Ò»¸öÊý×éÀȻºóͨ¹ý±éÀúÊý×éÀ´ÁгöËùÓÐÎļþ¡£
import java.io.*; public class ListContents { public static void main(String[] args) { File file = new File("//home//user//Documents/"); String[] files = file.list(); System.out.println("Listing contents of " + file.getPath()); for(int i=0 ; i < files.length ; i++) { System.out.println(files[i]); } } }Ò»¸ö¼òµ¥µÄIO³ÌÐò
¡¡