Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.factual.driver;
 
 import java.util.List;
 import java.util.Map;
 
public class JsonUtil {

  
Takes a JSONArray of records, where each record is a dictionary, and returns the translated List of Maps.
  public 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;
  }

  
Takes a JSONObject of records, where each record is a dictionary, and returns the translated Map of Maps.
  public static Map<StringMap<StringObject>> data(JSONObject jothrows JSONException {
    Map<StringMap<StringObject>> data = Maps.newHashMap();
    Iterator<?> iter = jo.keys();
    while (iter.hasNext()) {
      String key = iter.next().toString();
      data.put(keyrow(jo.getJSONObject(key)));
    }
    return data;
  }
  private static Map<StringObjectrow(JSONObject jothrows JSONException {
    return toMap(jo);
  }
  public static Map<StringObjecttoMap(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;
  }

  
Serialize a Java object to json. Use Maps and Lists to specify object and array structures respectively. For example, the following value as input:
 new HashMap() {{
 put("$and", new Map[] {
  new HashMap() {{
   put("name", new HashMap() {{
     put("$bw", "McDonald's");
    });
    put("category", new HashMap() {{
     put("$bw", "Food & Beverage");
    }});
  }}});
 }};
 }
 
Will be serialized to json as: {"$and":[{"name":{"$bw":"McDonald's"},"category":{"$bw":"Food & Beverage"}}

Parameters:
obj the object to be serialized as json
Returns:
the json string representing the passed in object
  public static String toJsonStr(Object obj) {
    try {
      return new ObjectMapper().writeValueAsString(obj);
    } catch (JsonGenerationException e) {
      throw new RuntimeException(e);
    } catch (JsonMappingException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
New to GrepCode? Check out our FAQ X