android数据解析之json解析和json字符串创建

构建一个android测试类说明json解析的应用和创建json字符串:


public class JsonAndroidTest extends AndroidTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}

public void jsonStr() throws JSONException {
String jsonStr = "{\"userName\":\"张三\",\"passWord\":123456}";
JSONObject jsonObject = new JSONObject(jsonStr);//实列化jsonStudent对象,获取json解析器,关联jsonStr
String userName = jsonObject.getString("userName");//解析jsonStr
int passWord = jsonObject.getInt("passWord");
Logs.v("userName:" + userName + ">>passWord" + passWord);
}


public void testJsonStudentstr() throws JSONException {
String jsonStr = "{\"student\":{\"name\":\"小明\",\"age\":18,\"id\":112,\"sex\":\"男\"}}";
JSONObject jsonObject = new JSONObject(jsonStr);
JSONObject jsonStudent = jsonObject.getJSONObject("student");
String name = jsonStudent.getString("name");
int age = jsonStudent.getInt("age");
Logs.v("name:" + name + ">>age" + age);
}


public void testJsonArray() throws JSONException {
String str = "{\"students\":[{\"name\":\"小明\",\"age\":18,\"id\":112,\"sex\":\"男\"},{\"name\":\"小花\",\"age\":16,\"id\":110,\"sex\":\"女\"}]}";
JSONObject jsonObject = new JSONObject(str);
JSONArray ja = jsonObject.getJSONArray("students");


int length = ja.length();
for (int i = 0; i < length; i++) {
JSONObject jobject = ja.getJSONObject(i);
String name = jobject.getString("name");
int age = jobject.getInt("age");
Logs.v("name:" + name + ">>age" + age);


}
}
//public void readHttpFile(){
// HttpConnectionUtil connect = new HttpConnectionUtil();
// connect.asyncTaskHttp("http://192.168.1.156/json/students.txt", Method., httpCallBack);
//
//}
//
public void readHttpFile(){

HttpConnectionUtil connect = new HttpConnectionUtil();
connect.asyncTaskHttp("http://192.168.1.167/app/print", Method.GET, new HttpCallBack() {

@Override
public void returnMessage(String message) {
Logs.v("message :"+message);
}
});
}





//创建json字符串


public void buildJsonObject() throws JSONException{
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "张三");
jsonObject.put("password", "123456");
Logs.v("生成Json字符串         "+jsonObject.toString());
}

public void buildJsonArrayObject() throws JSONException{
//{"students": [{"name":"小明","age":18,"id":112,"sex":"男"},{"name":"小王","age":17,"id":102,"sex":"女"} ] }

JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name", "小明");
jsonObject1.put("age", 18);
jsonObject1.put("id", 112);
jsonObject1.put("sex", "男");

JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", "小王");
jsonObject2.put("age", 17);
jsonObject2.put("id", 102);
jsonObject2.put("sex", "女");

JSONArray jsonArray = new JSONArray();
jsonArray.put(0,jsonObject1);
jsonArray.put(1,jsonObject2);

JSONObject jsonObject = new JSONObject();
jsonObject.put("students",jsonArray);

Logs.v("生成Json字符串         "+jsonObject.toString());

}

public void buildJsonByHashMap(){
HashMap<String, String> map = new HashMap<String, String>();
map.put("username", "小明");
map.put("password", "123456");

//{"username":"张三","password":123456}

JSONObject jsonObject = new JSONObject(map);
String str = jsonObject.toString();//将jsonObject对象转换为字符串
Logs.v("str:"+str);

}


public void readRawFile() throws IOException {
InputStream is = getContext().getAssets().open("jsonstr");

//InputStream is = getContext().getResources().openRawResource(R.raw.jsonstr);
String str = readIt(is);
Logs.v("str:" + str);


}


private String readIt(InputStream is) throws IOException {
// 创建包装流
Reader reader = new InputStreamReader(is, "UTF-8");


BufferedReader br = new BufferedReader(reader);
// 定义String类型用于储存单行数据c
String line = null;
// 创建StringBuffer对象用于储存所以数据
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
reader.close();


return sb.toString();
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值