?????????????????????JSON
????????????????????????????????????????java??????HTTP??????????????????????????????????Json????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????·??????
??????????????£?
public static String GetJsonValue(String result??int index??String key)
{
int indexloc??indexkey;
String newstr;
indexloc=result.indexOf("[");
indexkey=result.indexOf(key);
//?ж?Data???????
if (( indexloc>indexkey || indexloc==-1) & index==0)
{
JSONObject jsonObj = JSONObject.fromObject(result);
return jsonObj.getString(key);
}
else
{
newstr=GetNPro(result??index);
return GetJsonValue(newstr??0??key);
}
}
public static String  GetNPro(String str??int n)
{
Matcher slashMatcher = Pattern.compile("\{").matcher(str);
int mIdx = 0;
while(slashMatcher.find()) {
if(mIdx ==n){
break;
}
mIdx++;
}
str=str.substring(slashMatcher.start()??str.length());
return str.substring(0?? str.indexOf("}")+1);
}
????????????????????????????????????????Json??????????????????????????????????????????????????????????????????????磺GetJsonValue (jresult??0??”error”);???????????????data?л???????json?У???????????????????????????1??????????磺GetJsonValue (jresult??2??”name”) ??????????????????name??ε????
????????????????????????????????Key??????????????????????????????????????ж??????????????????????????????????????????????????????????????????????????????????
??????????????????
??????????????????????ú????????????????????????java??????????HTTP?????API????????????????????????????????£?
public static void main( String[] args )
{
// Get??????
String url="http://api.zhongchou.cn/deal/list";
String params="?v=1";
String apiresult=GetRequests(url??params);
System.out.println("errno:"+GetJsonValue(apiresult??0??"errno"));//????????????
System.out.println("name:"+GetJsonValue(apiresult??3??"name"));//?????????????????????
//Post??????
String posturl="http://api.zhongchou.cn/user/login?v=1";
Map map = new IdentityHashMap ();
map.put("identity"?? "183****8905");
map.put("password"?? "**********");
String poresult=PostRequests(posturl??map??null);
//?????????????????
System.out.println("Name:"+GetJsonValue(poresult??1??"name"));
}
????????????????????????????????????????????????java??????????????????????????????????????????????jar??????????
???????????????£???????????????????У????????????????????????????飬????????????????????????Щ??????????????????????????????????????????????????????????????????????