Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.factual;
  
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  
  import  org.json.JSONArray;
  import  org.json.JSONException;
 import  org.json.JSONObject;
 
Represents the response from running a fetch request against Factual, such as a geolocation based query for specific places entities.

Author(s):
aaron
 
 public class ReadResponse extends Response {
   private final String json;
   private List<Map<StringObject>> data = Lists.newArrayList();


  
Constructor, parses from a JSON response String.

Parameters:
json the JSON response String returned by Factual.
 
   public ReadResponse(String json) {
     this. = json;
     try{
       JSONObject rootJsonObj = new JSONObject(json);
       Response.withMeta(thisrootJsonObj);
        = data(rootJsonObj.getJSONObject("response").getJSONArray("data"));
     } catch (JSONException e) {
       throw new RuntimeException(e);
     }
   }
 
   @Override
   public String getJson() {
     return ;
   }
 
   public Map<StringObjectfirst() {
     if(.isEmpty()) {
       return null;
     } else {
       return .get(0);
     }
   }

  
An ordered collection of the main data returned by Factual. Represented as Maps, where each Map is a record in the results.

Returns:
the main data returned by Factual.
 
   public List<Map<StringObject>> getData() {
     return ;
   }

  

Returns:
the size of the result set
 
   public int size() {
     return .size();
   }

  

Returns:
a Collection of all String values found in this Response's data rows as the field attribute.
 
   public Collection<StringmapStrings(final String field) {
     return Collections2.transform(new Function<Map<StringObject>, String>() {
       @Override
       public String apply(Map<StringObjectrow) {
         Object val = row.get(field);
         return val != null ? val.toString() : null;
       }});
   }
 
   //TODO: Bradley: "mapToStrings, mapToDoubles, etc."
 
   private static List<Map<StringObject>> data(JSONArray arrthrows JSONException {
     List<Map<StringObject>> data = Lists.newArrayList();
     for(int i=0; i<arr.length(); i++) {
       data.add(row(arr.getJSONObject(i)));
     }
     return data;
   }
 
   private static Map<StringObjectrow(JSONObject jothrows JSONException {
    Map<StringObjectrow = Maps.newHashMap();
    Iterator<?> iter = jo.keys();
    while(iter.hasNext()) {
      String key = iter.next().toString();
      Object value = jo.get(key);
      row.put(keyvalue);
    }
    return row;
  }
New to GrepCode? Check out our FAQ X