} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else Toast.makeText(TestHttpActivity.this, "POST提交失败", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_get:
DefaultHttpClient client1 = new DefaultHttpClient();
/**NameValuePair是传送给服务器的请求参数param.get("name") **/
UrlEncodedFormEntity entity1=null;
/**新建一个get请求**/
HttpGet get = new HttpGet("");
HttpResponse response1=null;
String strResult1="";
try {
/**客服端向服务器发送请求**/
response1 = client1.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**请求发送成功,并得到响应**/
if(response1.getStatusLine().getStatusCode()==200){
try {
/**读取服务器返回过来的json字符串数据**/
strResult1 = EntityUtils.toString(response1.getEntity());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject1 = null;
try {
/**把json字符串转换成json对象**/
jsonObject1 = getJSON(strResult1);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String names="";
try {
/**
* jsonObject.getString("code") 取出code
* 比如这里返回的json 字符串为 [code:0,msg:"ok",data:[list:{"name":1},{"name":2}]]
* **/
/**得到data这个key**/
String data=https://www.04ip.com/post/jsonObject1.getString("data");
/**把data下的数据转换成json对象**/
JSONObject jDat1 = new JSONObject(data);
/**判断data对象下的list是否存在**/
if(jDat1.get("list")!=null){
/**把list转换成jsonArray对象**/
JSONArray jarr1 = jDat1.getJSONArray("list");
/**循环list对象**/
for (int i = 0; ijarr1.length(); i++) {
/** **/
JSONObject jsono = (JSONObject) jarr1.get(i);
/**取出list下的name的值 **/
names=names+jsono.getString("name");
}
}
Toast.makeText(TestHttpActivity.this,"get请求: code:"+jsonObject1.getString("code")+"name:"+names, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else Toast.makeText(TestHttpActivity.this, "get提交失败", Toast.LENGTH_SHORT).show();
break;
}
}
};
public JSONObject getJSON(String sb) throws JSONException {
return new JSONObject(sb);
}
}
VB如何获取json内容json 有专门的读取,但必须是标准Json
下面一段就可以用上面截图代码解析
【VB.NET】这个json怎么解析??用的是Newtonsoft.Json.dll先如下程序集
Imports System.Web.Script.Serialization
Function StringToObject(content As String) As Object
Dim jss As New JavaScriptSerializer()
Return jss.DeserializeObject(content)
End Function
Dim test1 As Dictionary(Of String, Object) = StringToObject("{
"Chr(34)"objects"Chr(34)": {
"Chr(34)"minecraft/sounds/entity/rabbit/attack2.ogg"Chr(34)": {
"Chr(34)"hash"Chr(34)": "Chr(34)"4b90ff3a9b1486642bc0f15da0045d83a91df82e"Chr(34)",
"Chr(34)"size"Chr(34)": 7268
},
"Chr(34)"minecraft/sounds/entity/shulker/close5.ogg"Chr(34)": {
"Chr(34)"hash"Chr(34)": "Chr(34)"e555fcece121879fe0becbada9d01dea27ab153d"Chr(34)",
"Chr(34)"size"Chr(34)": 5376
},
"Chr(34)"minecraft/lang/ga_IE.lang"Chr(34)": {
"Chr(34)"hash"Chr(34)": "Chr(34)"a108f19142820b3573a658d40950ba3317b65c15"Chr(34)",
推荐阅读
- 线上读书小程序开发实例,线上阅读书籍
- 苹果手机怎么清理软件缓存,苹果手机的缓存垃圾怎么清理
- 格斗冒险类手游游戏,格斗冒险类手游游戏推荐
- 单机游戏霹雳行动,老电影霹雳行动
- 滚动数组java代码 java swing滚动条
- 宜春批发零售小程序开发,宜春市零食批发
- 手机怎么调整路由器的速度,手机怎么调整路由器的速度和网速
- 如何在做ppt,如何在做PPT时将图片背景变透明
- python函数含调用 python函数中调用函数