Package com. gis. action; import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import net. sf. json. JSONObject; import org. apache. http. nameValuePair; import org. apache. http. message. basicNameValuePair; import org. codehaus. jackson. map. objectMapper; import com. gis. entity. attributes; import com. gis. entity. geometry; import com. gis. entity. heavyMetal; import com. gis. rest. restAPI; import com. gis. util. constants; public class QueryAction {public jsonquerybyobjectid (int objectid) {HeavyMetal = new heavyMetal (); List <strong> formparams = new ArrayList <strong> (); formparams. add (new BasicNameValuePair ("f", "json"); // queryurl = http: // 192.168.24.218/ArcGIS/rest/services/HeavyMetal/FeatureServer/0/11 (11 is objectid) String response = RestAPI. rest (Constants. queryurl + objectid, formparams); JSONObject jsonObject = JSONObject. fromObject (response); Map map = new HashMap (); for (Iterator iter = jsonObject. keys (); iter. hasNext ();) {String key = (String) iter. next (); map. put (key, jsonObject. get (key);} System. out. println ("feature:" + map. get ("feature"); ObjectMapper objectMapper = new ObjectMapper (); String str = map. get ("feature "). toString (); JSONObject jsonObject1 = JSONObject. fromObject (str); Map map1 = new HashMap (); String [] strings = new String [2]; int I = 0; for (Iterator iter = jsonObject1.keys (); iter. hasNext ();) {// System. out. println (iter); String key = (String) iter. next (); map. put (key, jsonObject1.get (key); // System. err. println (jsonObject1.get (key); strings [I] = jsonObject1.get (key ). toString (); I ++;} JSONObject obj = new JSONObject (). fromObject (strings [0]); // System. err. println ("OBJECTID:" + obj. get ("OBJECTID"); Attributes attributes = (Attributes) JSONObject. toBean (obj, Attributes. class); obj = new JSONObject (). fromObject (strings [1]); Geometry geometry = (Geometry) JSONObject. toBean (obj, Geometry. class); heavyMetal. setAttributes (attributes); heavyMetal. setGeometry (geometry); return heavyMetal;}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub QueryAction queryAction = new QueryAction (); System. err. println (queryAction. queryByObjectid (11 ));}}
public class RestAPI { public static String rest(String url,List<NameValuePair> formparams) { HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); String responseBody = null; BufferedReader br = null; try { UrlEncodedFormEntity uef = new UrlEncodedFormEntity(formparams, HTTP.UTF_8); post.setEntity(uef); ResponseHandler<String> responseHandler = new BasicResponseHandler(); responseBody = httpClient.execute(post, responseHandler); System.out.println(responseBody); } catch (Exception e) { // TODO: handle exception } return responseBody; }}
Response value
{"Feature": {"attributes": {"OBJECTID": 11, "ID": 1, "CD": 0.2433, "HG": 0.2433, "DP_AS ": 1.0309, "CU": 2.3848, "PB": 217.214, "CR": 353.527, "ZN": 357.4679, "NI": 208.2775, "HM_TIME": 1379085429000, "HM_NAME": "Yichang", "USER_NAME": null}, "geometry": {"x": 111.300000000745, "y": 31.1999999992549 }}}
Str value after one resolution
Feature: {"attributes": {"OBJECTID": 11, "ID": 1, "CD": 0.2433, "HG": 0.2433, "DP_AS": 1.0309, "CU": 2.3848, "PB": 217.214, "CR": 353.527, "ZN": 357.4679, "NI": 208.2775, "HM_TIME": 1379085429000, "HM_NAME ": "Yichang", "USER_NAME": null}, "geometry": {"x": 111.3, "y": 31.2 }}
Then, attributes and geometry are parsed respectively to get the parsed object.
Demo address http://down.51cto.com/data/947003
This article is from the "mic" blog, please be sure to keep this source http://umgsai.blog.51cto.com/2079212/1297385