Parsing json object into a string

2019-04-27 12:31发布

问题:

I have a question regarding a web-application I'm building where I have a REST service receiving a json string.

The Json string is something like:

{
    "string" : "value",
    "string" : "value", 
    "object" : {
                 "string" : "value",
                 "string" : "value",
                 ....
                }
}

I'm using resteasy to parse the json string which uses jackson underneath. I have a jaxb annotated class and I want to parse the "object" entirely into a String variable. The reason I want to do this is to be able to parse the json later using the correct parser (it depends on the application that sends the request so it is impossible to know in advance).

My jaxb annotated class looks like this:

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Test{

@XmlElement(type = String.class)
private String object;

//getter and setter
...
}

When I execute the rest call and let jackson parse this code I get an

Can not deserialize instance of java.lang.String out of START_OBJECT token

error. So actually I'm trying to parse a piece of a json string, which is a json object, into a String. I can't seem to find someone with a similar problem.

Thanks in advance for any response.

回答1:

If I understand this question you just want a mechnanism, that converts a Java-Object into a JSON-String and the other way.

I needed this as well, while I was using a WebSocket Client-Server communication where a JSON String has been passed around.

For this I used GSON (see GSON). There you got the possibility to create a complete JSON-String. Here some example:

// Converts a object into a JSON-String
public String convertMyClassObjectToJsonFormat() {
  MyClass myObject = new MyClass();
  Gson gson = new Gson();

  return gson.toJson(myObject);
}

//Converts a JSON-String into a Java-Class-Object
public MyClass convertJsonToMyClassObject(
            CharBuffer jsonMessage) {
  Gson gson = new Gson();

  return gson.fromJson(jsonMessage.toString(),
                MyClass.class);
}

What you need is, that you your Class-Attributes-setter and JSON-Attribute-names are equivalent. E.g.

{
   "info":[
      {
         "name": "Adam",
         "address": "Park Street"
      }
    ]
}

Your Class should look like this:

public class Info{
 private String name;
 private String address;

 public void setName(String name){
  this.name = name;
 }
 public void setAddress(String address){
  this.address = address;
 }
}


回答2:

@KwintenP Try using the json smart library.

You can then simply retrieve the JSON object first using:

JSONObject test = (JSONObject) JSONValue.parse(yourJSONObject);
String TestString = test.toString();

What's more, you can retrieve a specific object inside a JSON object may it be another object, an array and convert it to a String or manipulate the way you want.



回答3:

java.lang.String out of START_OBJECT token

this means that expected character after "object" is quotes ", but not brackets {.

Expected json

"object" : "my object"

Actual json

"object" : { ...  

=======
If you want parse json like in your example, then change your class. E.g.

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Test{

   @XmlElement
   private InnerTest object;

   //getter and setter
...
}  

@XmlAccessorType(XmlAccessType.PROPERTY)
public class InnerTest{

   @XmlElement
   private String string;

   //getter and setter
...
}


回答4:

you also can do something like this ;

public class LeaderboardView
{
     @NotEmpty
     @JsonProperty
     private String appId;

     @NotEmpty
     @JsonProperty
     private String userId;

     @JsonProperty
     private String name = "";

     @JsonProperty
     private String imagePath = "";

     @NotEmpty
     @JsonIgnore
     private String rank = "";

     @NotEmpty
     @JsonProperty
     private String score;

     public LeaderboardView()
     {
        // Jackson deserialization
     }

}