2017-05-05 63 views
2

我需要將AWS DYNAMODB JSON轉換爲標準JSON對象。 所以我才能把DynamoDB JSON 更多的東西一樣的數據類型:在DYNAMODB JSON使用Java將DynamoDB JSON轉換爲標準JSON

"videos": [ 
    { 
     "file": { 
     "S": "file1.mp4" 
     }, 
     "id": { 
     "S": "1" 
     }, 
     "canvas": { 
     "S": "This is Canvas1" 
     } 
    }, 
    { 
     "file": { 
     "S": "main.mp4" 
     }, 
     "id": { 
     "S": "0" 
     }, 
     "canvas": { 
     "S": "this is a canvas" 
     } 
    } 
    ] 

to Standard JSON 
"videos": [ 
    { 
     "file": "file1.mp4" 
     , 
     "id": "1" 
     , 
     "canvas": "This is Canvas1" 
     , 
     "file": "main.mp4" 
     , 
     "id": "0" 
     , 
     "canvas": "this is a canvas" 

    } 
    ] 

我發現了一個很好的工具在Javascript,但有沒有在Java中,以做任何工具那?

回答

2

下面是來自迪納摩JSON轉換爲標準的JSON的完整代碼:

import com.amazonaws.services.dynamodbv2.document.Item; 
import com.amazonaws.services.dynamodbv2.document.internal.InternalUtils; 
import com.amazonaws.services.dynamodbv2.model.AttributeValue; 
import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.RequestHandler; 
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent; 
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord; 
import com.google.gson.Gson; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

/** 
* Main Lambda class to receive event stream, parse it to Survey 
* and process them. 
*/ 
public class SurveyEventProcessor implements 
     RequestHandler<DynamodbEvent, String> { 

    private static final String INSERT = "INSERT"; 

    private static final String MODIFY = "MODIFY"; 

    public String handleRequest(DynamodbEvent ddbEvent, Context context) { 

     List<Item> listOfItem = new ArrayList<>(); 
     List<Map<String, AttributeValue>> listOfMaps = null; 
     for (DynamodbStreamRecord record : ddbEvent.getRecords()) { 

      if (INSERT.equals(record.getEventName()) || MODIFY.equals(record.getEventName())) { 
       listOfMaps = new ArrayList<Map<String, AttributeValue>>(); 
       listOfMaps.add(record.getDynamodb().getNewImage()); 
       listOfItem = InternalUtils.toItemList(listOfMaps); 
      } 

      System.out.println(listOfItem); 
      try { 
       // String json = new ObjectMapper().writeValueAsString(listOfItem.get(0)); 
       Gson gson = new Gson(); 
       Item item = listOfItem.get(0); 

       String json = gson.toJson(item.asMap()); 
       System.out.println("JSON is "); 
       System.out.println(json); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 


     return "Successfully processed " + ddbEvent.getRecords().size() + " records."; 
    } 
} 
1

下面是一個簡單的解決方案,它可以應用於將任何DynamoDB Json轉換爲Simple JSON。

//passing the reponse.getItems() 
public static Object getJson(List<Map<String,AttributeValue>> mapList) { 
    List<Object> finalJson= new ArrayList(); 
    for(Map<String,AttributeValue> eachEntry : mapList) { 
     finalJson.add(mapToJson(eachEntry)); 
    } 
    return finalJson; 
} 


//if the map is null then it add the key and value(string) in the finalKeyValueMap 
public static Map<String,Object> mapToJson(Map<String,AttributeValue> keyValueMap){ 
    Map<String,Object> finalKeyValueMap = new HashMap(); 
    for(Map.Entry<String, AttributeValue> entry : keyValueMap.entrySet()) 
    { 
     if(entry.getValue().getM() == null) { 
      finalKeyValueMap.put(entry.getKey(),entry.getValue().getS()); 
     } 
     else { 
      finalKeyValueMap.put(entry.getKey(),mapToJson(entry.getValue().getM())); 
     } 
    } 
    return finalKeyValueMap; 
} 

這將以List>的形式生成所需的Json。然後使用Gson,你可以將它轉換成Json格式。

Gson gson = new Gson(); 
String jsonString = gson.toJson(getJson(response.getItems())); 
1

您可以在aws sdk中使用ItemUtils類。下面是一個使用科特林示例代碼:

import com.amazonaws.services.dynamodbv2.document.ItemUtils 
import com.amazonaws.services.dynamodbv2.model.AttributeValue 

fun main(args: Array<String>) { 
    val data = HashMap<String,AttributeValue>() 
    data.put("hello",AttributeValue().withS("world")) 
    println(data.toString()) 
    println(ItemUtils.toItem(data).toJSON()) 
} 

輸出:

{hello={S: world,}} 
{"hello":"world"}