File tree Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change
1
+ use serde:: { Deserialize , Serialize } ;
2
+
3
+ #[ derive( Serialize , Deserialize ) ]
4
+ struct Paragraph {
5
+ name : String ,
6
+ }
7
+
8
+ #[ derive( Serialize , Deserialize ) ]
9
+ struct Article {
10
+ title : String ,
11
+ author : String ,
12
+ paragraph : Vec < Paragraph > ,
13
+ }
14
+
1
15
fn main ( ) {
2
- println ! ( "Hello, world!" ) ;
16
+ let json = r#"
17
+ {
18
+ "title": "To kill a monkey",
19
+ "author": "Kemi Adetiba",
20
+ "paragraph":[
21
+ { "name": "Starting plot" },
22
+ { "name": "body of the paragraph" },
23
+ { "name": "end of the paragraph" }
24
+ ]
25
+ }"# ;
26
+
27
+ let parsed = read_json_typed ( json) ;
28
+ println ! ( "\n \n The name of the first paragraph is: {}" , parsed. paragraph[ 0 ] . name) ;
29
+ }
30
+
31
+ fn read_json_typed ( raw_json : & str ) -> Article {
32
+ let parsed = serde_json:: from_str ( raw_json) . unwrap ( ) ;
33
+ return parsed
3
34
}
You can’t perform that action at this time.
0 commit comments