File tree Expand file tree Collapse file tree 7 files changed +87
-3
lines changed Expand file tree Collapse file tree 7 files changed +87
-3
lines changed Original file line number Diff line number Diff line change 53
53
<artifactId >fastjson</artifactId >
54
54
<version >1.2.47</version >
55
55
</dependency >
56
+ <dependency >
57
+ <groupId >junit</groupId >
58
+ <artifactId >junit</artifactId >
59
+ <version >4.13.1</version >
60
+ <scope >test</scope >
61
+ </dependency >
56
62
</dependencies >
57
63
58
64
<build >
Original file line number Diff line number Diff line change
1
+ import java .io .FileNotFoundException ;
2
+ import java .io .IOException ;
3
+ import java .io .RandomAccessFile ;
4
+ import java .util .ArrayList ;
5
+
6
+ public class RAFile {
7
+ public ArrayList <String > content ;
8
+ public RAFile (String path ) throws IOException {
9
+ RandomAccessFile rf = new RandomAccessFile (path , "r" );
10
+ content = new ArrayList <>();
11
+ while (rf .getFilePointer () < rf .length ()) {
12
+ content .add (rf .readLine ());
13
+ }
14
+ rf .close ();
15
+ }
16
+
17
+ public String toString () {
18
+ StringBuilder res = new StringBuilder ();
19
+ for (String s : content ) {
20
+ res .append (s ).append ("\n " );
21
+ }
22
+ return res .toString ();
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package translate ;
2
+
3
+ import com .alibaba .fastjson .JSONObject ;
4
+ import com .alibaba .fastjson .parser .Feature ;
5
+
6
+ import java .util .LinkedHashMap ;
7
+ import java .util .Map ;
8
+
9
+ public class NewJSONObject {
10
+ public Map <String , String > map ;
11
+ public NewJSONObject () {
12
+ this .map = new LinkedHashMap <>(); //new HashMap();
13
+ }
14
+ public static NewJSONObject parseObject (String content , Feature orderedField ) {
15
+ NewJSONObject object = new NewJSONObject ();
16
+ JSONObject jsonObject = JSONObject .parseObject (content );
17
+ for (String key : jsonObject .keySet ()) {
18
+ object .map .put (key , jsonObject .getString (key ));
19
+ }
20
+ return object ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change 1
1
package translate ;
2
2
3
3
import com .alibaba .fastjson .JSONObject ;
4
+ import com .alibaba .fastjson .parser .Feature ;
4
5
5
6
import java .io .FileNotFoundException ;
6
7
import java .io .IOException ;
7
8
import java .io .RandomAccessFile ;
9
+ import java .util .LinkedHashMap ;
10
+ import java .util .Map ;
8
11
9
12
public class Trans {
13
+ public JSONObject object ;
10
14
public Trans (String path ) throws IOException , FileNotFoundException {
11
15
RandomAccessFile rf = new RandomAccessFile (path , "r" );
12
16
StringBuilder content = new StringBuilder ();
13
17
while (rf .getFilePointer () < rf .length ()) {
14
18
content .append (rf .readLine ());
15
19
}
16
20
rf .close ();
17
- JSONObject object = JSONObject .parseObject (content .toString ());
18
- System .out .println (object );
21
+ object = JSONObject .parseObject (content .toString (), Feature .OrderedField );
19
22
}
20
23
public static void main (String [] args ) throws IOException , FileNotFoundException {
21
- Trans trans = new Trans ("test .json" );
24
+ Trans trans = new Trans ("in .json" );
22
25
}
23
26
}
Original file line number Diff line number Diff line change
1
+ import org .junit .Test ;
2
+ import translate .Trans ;
3
+
4
+ import static org .junit .Assert .assertTrue ;
5
+ import static org .junit .Assert .fail ;
6
+
7
+ public class TransClassTest {
8
+ @ Test
9
+ public void testTrans () throws Exception {
10
+ Trans trIn = new Trans ("test/TransClass/in.json" );
11
+ RAFile raIn = new RAFile ("test/TransClass/out.dat" );
12
+ int i = 0 ;
13
+ for (String key : trIn .object .keySet ()) {
14
+ if ((trIn .object .get (key )).equals (raIn .content .get (i ))) {
15
+ i ++;
16
+ } else {
17
+ fail ();
18
+ }
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "ShangHai" : " 上海" ,
3
+ "Pudong" : " 浦东" ,
4
+ "ShangHai-Pudong" : " 上海-浦东"
5
+ }
Original file line number Diff line number Diff line change
1
+ 上海
2
+ 浦东
3
+ 上海-浦东
You can’t perform that action at this time.
0 commit comments