-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonActivity.java
More file actions
63 lines (49 loc) · 1.77 KB
/
JsonActivity.java
File metadata and controls
63 lines (49 loc) · 1.77 KB
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
package com.example.androidtest;
import org.json.JSONException;
import org.json.JSONObject;
import org.xframe.annotation.JSONUtils;
import org.xframe.annotation.JSONUtils.JSONDict;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class JsonActivity extends Activity {
public static class Base {
@JSONDict(name = "s", defVal = "")
public String s;
}
public static class Test extends Base {
@JSONDict(name = "a", defVal = "-1")
public int a;
@JSONDict(name = "b", defVal = "0", type = Float.class)
public float b;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
TextView tView = (TextView) findViewById(R.id.textview);
// JSONTokener jt = new JSONTokener("12342314234231423");
// try {
// tView.setText(jt.nextValue().getClass().toString());
// } catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
String j1 = "{a: 123, b: 1.2378, s: '12345'}";
try {
Test t = new Test();
JSONUtils.json2JavaObject(new JSONObject(j1), t);
tView.setText(String.format("%s\n%d\n", t.s, t.a) + t.b);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.json, menu);
return true;
}
}