-7

My requirement is to create a map which contains key as tableName and value as elements inside the schema field in the json file.

{
  "status":"success",
  "tables":[
      {
         "dbname":"idn",
        "tableName":"my_monthly_hits_b",
        "schema":"(cm11:chararray)",
        "location":"/user/mydb/"
      },
      {
         "dbname":"idn",
         "tableName": "my_monthly_match",
         "schema":"(city:chararray,match:chararray,cm11:chararray)",
         "location":"/user/mydb1"
      }
   ]
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Deb
  • 5
  • 4
  • hey, looks like you're new to stackoverflow. Check this out first: http://stackoverflow.com/help/on-topic – galdin Apr 27 '15 at 09:42

3 Answers3

0

I tried the below code.But I dont want to do the string manipulation.Is there a way to get the put the TableMane and schema into the map without doing string manipulation.

public Map<String,Map<String,String>> getDataTypes(String responsePath){
  Map<String,Map<String,String>> maped = new HashMap<String,Map<String,String>>();
Map<String,String> colDataTypes = new HashMap<String,String>();
try{
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader(responsePath);
JsonObject jObj = (JsonObject) obj;
JsonArray jArray = (JsonArray) jObj.get("tables");
Iterator<JsonElement> itr = jArray.iterator();

while(itr.hasNext())
{
    JsonObject  innerObj = (JsonObject) itr.next();
    JsonElement shm = innerObj.get("schema");
    JsonElement jTableName = innerObj.get("tableName");
    String tableName = jTableName.toString();
    String ss = shm.toString().replaceAll("\"","").replaceAll("[()]",""):
    System.out.println("The required JSON string --->" + ss);
    if(ss.contains(","){
       String[] str = ss.split(",");
       for(String s: str){
          String[] ptr = s.split(":");
          colDataTypes.put(prt[0],ptr[1]);
       }
   }
   else{
       String[] str1 = ss.split(":");
       colDataTypes.put(str1[0],str1[1]);
  }
  maped.put(tabName,colDataTypes);
  for(String tab : maped.keySet()){
     System.out.println("#####" + "Table Name " + tab + "value" + maped.get(tab));
}
}
}
catch(FileNotFoundException ex)
{
}
return maped;
} 
Deb
  • 5
  • 4
0

There are several tools that could parse the json into a java class, if you find it easier to travers. For example fasterxml ObjectMapper. http://wiki.fasterxml.com/JacksonInFiveMinutes

Then this json could be populated into a java class, eg

class MyInput{
  private String status;
  private MyTable[] tables;
  ...
}
class MyTable{
  String dbname;
  String tableName;
  ...
}

and once fasterxml populates it for you, you can toop over the tables and do what you want with them (e.g build a map)

Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11
0

Use the GSON library.

Your model classes will be something like this:

public class Table
{
    private String dbname;
    private String tableName;
    private String schema;
    private String location;

    public String getDbname()
    {
        return dbname;
    }
    public String setDbname(String value)
    {
        dbname = value;
    }

    // ... add getters and setters for the remaining fields too
}

public class JsonRoot  // give it an appropriate name
{
    private String status;
    private List<Table> tables;

    // ... add getters and setters for the above fields
}

Using the GSON library you'll be able to convert the json string into a JsonRoot object.

Useful links:
https://github.com/google/gson http://search.maven.org/#artifactdetails|com.google.code.gson|gson|2.3.1|jar

galdin
  • 12,411
  • 7
  • 56
  • 71