简体   繁体   English

如何在java中拆分字符串并创建一个json对象?

[英]How to split a string and create a json object in java?

I'm trying to split a string and create a json object with it.我正在尝试拆分一个字符串并用它创建一个 json 对象。 But somehow I can't create the json object.但不知何故我无法创建 json 对象。 Json object "objectBuilder" doesn't get the value. Json 对象“objectBuilder”没有得到值。 Please help.请帮忙。

I have book number like: "bookNo":"120, 130, 140".我有这样的书号:“bookNo”:“120, 130, 140”。 Here bookNo could be change and comes with 2 values or more.此处 bookNo 可以更改并带有 2 个或更多值。 These book no., I'm getting from the database.这些书号,我是从数据库中得到的。 And I can't change the Json API.而且我无法更改 Json API。 It should be javax.json api.它应该是 javax.json api。

    for(BookTerm dataTable:list){
        URL url1 = new URL("http://books.google.com/type/");
        JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
        String uriAdd = dataTable.getBookNo().toString();
        String[] uriLink = uriAdd.split(", ");
        int i = 0;
        String uriLink1 = null;
           if(uriLink != null){
               while (i< uriLink.length){
                   uriLink1 = uriLink[i];
                   URL url2 = new URL(url1.getProtocol(), url1.getHost(), url1.getPort(), url1.getPath() + uriLink1, null);
                   uriAdd1 = url2.toString();
                   i++;
                   objectBuilder.add("bookNumber", uriLink1)
                      .add("uri", uriAdd1).build();
               }
           }
    }

Output will be:输出将是:

    {
         "bookNumber":"120",
         "uri":"http://books.google.com/type/120"
    },
    {
         "bookNumber":"130",
         "uri":"http://books.google.com/type/130"
    }

and so on.等等。

Create a POJO class with attributes bookNumber and uri and override toString method just like :创建一个具有属性bookNumberuri的 POJO 类并覆盖 toString 方法,就像:

public class Book{
private String bookNumber;
private String uri;

//define a constructor with parameters
public Book(String bookNumber, String uri){
this.bookNumber = bookNumber;
this.uri=uri;
}


// define getters setters
// public String getBooknumber(..) etc.


@Override
    public String toString(){
        return "obj{bookNumber:" + "\"" + this.bookNumber + "\"" +", uri:"+ "\"" +  this.uri }" ;
    }
}

In your class make an instance of Book class and create a JSONObject:在你的类中创建一个 Book 类的实例并创建一个 JSONObject:

Book book = new Book("123214213", "http://www.sth.com");
JSONObject jsobject = new JSONObject(book.toString());

JSONArray jsonArray = new JSONArray();
jsonArray.put(jsobject);

//.....

I have never used JsonObjectBuilder before but it looks like you are never saving a reference to the JsonObject that the builder created on your line with .build().我以前从未使用过 JsonObjectBuilder,但看起来您从未保存对构建器使用 .build() 在您的生产线上创建的 JsonObject 的引用。 Also, you are constantly adding the same key to the builder's object, simply overwriting it each time.此外,您不断向构建器的对象添加相同的键,每次只需覆盖它。 You want to create an ArrayBuilder and have multiple ObjectBuilders inside of it like this:您想创建一个 ArrayBuilder 并在其中包含多个 ObjectBuilder,如下所示:

JsonBuilderFactory factory = Json.createBuilderFactory();
JsonObject value = factory.createArrayBuilder();

while(haveObjects) {
   value.add(Json.createObjectBuilder().add("bookNumber", uriLink1).add("uri", uriAdd1).build());
}
// writer will be constructed with some sort of output, and this will write the JSON value to it.
JsonWriter writer = Json.createWriter();
writer.write(value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM