位置:首頁 > 軟件操作教程 > 編程開發(fā) > Java > 問題詳情

Java操作應(yīng)用——JSON解析

提問人:ylm發(fā)布時間:2020-09-29
<track id="vrnpp"><nobr id="vrnpp"></nobr></track>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

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  

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(fù)(0)

    <var id="vrnpp"><kbd id="vrnpp"></kbd></var>