简体   繁体   English

从Java哈希图中提取特定键的值

[英]Extract value of a particular key from java hashmap

I am making a POST call and the response that I am getting is: 我正在进行POST呼叫,得到的响应是:

{"atoken":"32ab3a8738a4b57165b5fc863af4f4c95198c2c9","utoken":"9a3682e9eac186d56b230d075e7dc0d784992231"}

The code that is being used for making the POST call is: 用于进行POST调用的代码是:

try{
                                        // Create a new HttpClient and Post Header
                                        HttpClient httpclient = new DefaultHttpClient();
                                        HttpPost httpPost3 = new HttpPost("http://www.mywebsite.com/adkh.json");

                                        Log.i("Inside TokenPost", "Inside TokenPost");
                                            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);


                nameValuePair.add(new BasicNameValuePair("mobile", "989876556789"));
                nameValuePair.add(new BasicNameValuePair("imei", "911239987651238"));
                nameValuePair.add(new BasicNameValuePair("stoken", mtokenno));
                                             // 
                             try {
                                    httpPost3.setEntity(new UrlEncodedFormEntity(nameValuePair));
                                            } catch (UnsupportedEncodingException e) {
                                                // writing error to Log
                                                e.printStackTrace();
                                            }
                                           //

                              // Execute HTTP Post Request
                              try{
                              HttpResponse response = httpclient.execute(httpPost3);
                              Log.i("Response from Http Post Request", "Response from Http Post Request: "+ response);
                              System.out.println("Response: " +response.toString());

                              Log.i("Starting BufferedReader", "Starting BufferedReader");
                                           BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                                           StringBuilder builder = new StringBuilder();
                                           for (line = null; (line = reader.readLine()) != null;) {
                                               builder.append(line).append("\n");

                                               System.out.println(line); 

I am converting the response from the above code into java hash: 我将上述代码的响应转换为Java哈希:

class NaturalDeserializer implements JsonDeserializer<Object> {
                                              public Object deserialize(JsonElement json, Type typeOfT, 
                                                  JsonDeserializationContext context) {
                                                if(json.isJsonNull()) return null;
                                                else if(json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive());
                                                else if(json.isJsonArray()) return handleArray(json.getAsJsonArray(), context);
                                                else return handleObject(json.getAsJsonObject(), context);
                                              }
                                              private Object handlePrimitive(JsonPrimitive json) {
                                                if(json.isBoolean())
                                                  return json.getAsBoolean();
                                                else if(json.isString())
                                                  return json.getAsString();
                                                else {
                                                  BigDecimal bigDec = json.getAsBigDecimal();
                                                  // Find out if it is an int type
                                                  try {
                                                    bigDec.toBigIntegerExact();
                                                    try { return bigDec.intValueExact(); }
                                                    catch(ArithmeticException e) {}
                                                    return bigDec.longValue();
                                                  } catch(ArithmeticException e) {}
                                                  // Just return it as a double
                                                  return bigDec.doubleValue();
                                                }
                                              }
                                              private Object handleArray(JsonArray json, JsonDeserializationContext context) {
                                                Object[] array = new Object[json.size()];
                                                for(int i = 0; i < array.length; i++)
                                                  array[i] = context.deserialize(json.get(i), Object.class);
                                                return array;
                                              }
                                              public Object handleObject(JsonObject json, JsonDeserializationContext context) {
                                                Map<String, Object> map = new HashMap<String, Object>();
                                                for(Map.Entry<String, JsonElement> entry : json.entrySet())

                                               //System.out.println(map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class)));
                                               map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class));
                                               //Log.i("Tango Inside Deserialize", "Tango Inside Deserialize");     
                                                return map;
                                              }
                                            @Override
                                            public Object deserialize(JsonElement arg0,
                                                    java.lang.reflect.Type arg1,
                                                    JsonDeserializationContext arg2)
                                                    throws JsonParseException {
                                                // TODO Auto-generated method stub.
                                                return null;
                                            }
                                            }
                                            GsonBuilder gsonBuilder = new GsonBuilder();
                                            gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer());
                                            Gson gson = gsonBuilder.create();

                                            Object natural = gson.fromJson(line, Object.class);
                                            //System.out.println(json);
                                            System.out.println(natural);

                                            Log.i("NaturalDeserializer", "NaturalDeserializer");

Java hash that I am getting: 我得到的Java哈希:

{atoken=32ab3a8738a4b57165b5fc863af4f4c95198c2c9, utoken=9a3682e9eac186d56b230d075e7dc0d784992231}

Now, I need to make a POST call to: 现在,我需要进行POST调用:

curl --data "utoken=7d02a59410722737566f33879dcd061d5f45bc5a&lat=26.850307&lng=80.916853" http://www.website.com/khgt

For which I need to have the value of utoken, I am not sure how to extract the value of utoken. 对于我需要具有utoken的值,我不确定如何提取utoken的值。 Could anyone please help? 谁能帮忙吗?

如果您有HashMap,则应该可以使用get来获取令牌:

String utoken = map.get("utoken");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM