【问题标题】:Parse Json with com faster jackson dependencies使用 com 更快的杰克逊依赖项解析 Json
【发布时间】:2017-02-06 17:58:32
【问题描述】:

我想解析这个 json 对象数据,而不是逐行将数据插入数据库:

这是我的依赖:

"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"
    }
]

提前谢谢你。

【问题讨论】:

  • 所以你想让我们为你写一个pojo?您在研究文档时到底有什么不明白的?
  • 我想看看如何将此 XML 转换为我可以逐行放入 ebean 模型(数据库)的东西....并且肯定使用 java 控制器!!!跨度>

标签: javascript java json


【解决方案1】:

这是我为解决我的问题所做的:

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 Route(从 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 发送到 CONTROLLER 的 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);



}

【讨论】:

  • 谢谢你uuuuuuuuuu 这正是我正在寻找的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 2022-07-21
  • 2021-11-01
相关资源
最近更新 更多