2017-02-06 81 views
-2

我想解析此JSON對象數據,而不是用一行行將數據插入到數據庫:解析JSON與COM更快傑克遜相依

這裏是我的依賴關係:

"com.fasterxml.jackson.core" % "jackson-databind" % "2.8.6", // Json 
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.8.6", 
"com.fasterxml.jackson.core" % "jackson-core" % "2.8.6" 

,這裏是我的例子:

[ 
    { 
     "td1": "id", 
     "td2": "first_name", 
     "td3": "last_name" 
    }, 
    { 
     "td1": "1", 
     "td2": "First Name 1", 
     "td3": "Last Name 1" 
    }, 
    { 
     "td1": "2", 
     "td2": "First Name 2", 
     "td3": "Last Name 2" 
    } 
] 

謝謝你提前。

+1

所以,你想我們爲你寫一個pojo?在研究文檔時,你究竟有什麼不明白的地方? – baao

+0

我試圖看看如何將這個XML轉換成我可以逐行放入ebean模型(數據庫)....並確保使用java控制器! –

回答

0

這是我做了什麼來解決我的問題:

FI:進出口工作Play上的框架2.5.X:

1-視圖(按鈕)

<button type="button" class="btn btn-primary btn-xs" onclick="JS_ADD(this,'Add ALIAS')"><i class="fa fa"></i>Save</button> 

2-查看(表)

<table class="myTable" id="myTable" cellspacing="0"> 

    <tbody> 
    <tr> 
    <th scope="col">Code</th> 
    <th scope="col">Description</th> 
    <th scope="col">Actions</th> 
    </tr> 

@for(al <- parameterModel.alias){ 
    <tr> 
    <th scope="row" class="spec"><input type="checkbox" name="id" class="minimal" value="@al.id"></th> 
    <td><input type="text" name='name' value='@al.name' placeholder='Name' class="form-control"/></td> 
    <td></td> 
    </tr> 
} 

</tbody> 
</table> 

3-使用Javascript(在JS我們戈納發送表的可編輯爲JSON到控制器)

function JS_ADD(elem,callurl) 
{ 
if(callurl == 'Add ALIAS') 
{ 


var parameterID = document.getElementById('parameterID').value; 

var newFormData=[]; 
    jQuery('#myTable tr:not(:first)').each(function(i){ 
     var tb=jQuery(this); 
     var obj={}; 
     tb.find('input').each(function(){ 
      obj[this.name]=this.value; 
     }); 
     // obj['row']=i; 
     newFormData.push(obj); 

    }); 



    $.ajax 
    ({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: "/parameters/alias/"+parameterID, 
    data: JSON.stringify(newFormData), 
     success: function(data) 
     { 
     if(data.error) return; 
     $(document).ajaxStop(function() { location.reload(); }); 
     } 
    }); 


} 
} 

4- CONF路線(路由器從AJAX重定向URL)

POST  /parameters/alias/:id   controllers.ParametersController.jsonAlias(id:Long) 

5-最後控制器添加或不添加新值...等

public Result jsonAlias(Long parameterID) 
{ 

JsonNode json = request().body().asJson(); 
ObjectMapper objectMapper = new ObjectMapper(); 


try { 

List<Alias> list = objectMapper.readValue(json.toString(),TypeFactory.defaultInstance().constructCollectionType(List.class, Alias.class)); 

    for(Alias x : list) 
    { 

     //Update 
     if(x.id != null) 
     {      
     Alias a = Alias.find.byId(x.id); 
     a.name = x.name; 
     a.parameters = Parameters.find.byId(parameterID); 
     a.update(); 
     } 

     //Insert 
     else 
     { 
     Alias a = new Alias(); 
     a.id = null; 
     a.name = x.name; 
     a.parameters = Parameters.find.byId(parameterID); 
     a.save(); 
     } 

    } 

} catch (IOException e) {e.printStackTrace();} 


return GO_HOME; 
} 

--->我一直在尋找解析從VIEW到控制器發出的我JSON數據的方法是這樣的:

List<Alias> list = objectMapper.readValue(json.toString(),TypeFactory.defaultInstance().constructCollectionType(List.class, Alias.class)); 

儘量不要忘記你的模型中添加這個太:

//private List<Alias> listalias; 

import io.ebean.Finder; 

@Entity 
@Table(name = "alias") 
public class Alias extends Model 
{ 
    @Id 
    public Long id; 

    @Constraints.Required 
    @Formats.NonEmpty 
    public String name; 

    @ManyToOne 
    public Parameters parameters; 

    // This List will take Json as Object Model 
    private List<Alias> listalias; 


    public static final Finder<Long, Alias> find = new Finder<>(Alias.class); 



} 
+0

謝謝youuuuuuuuuuuu這麼多,這正是我在找什麼:) –