Parsing json object into string

I have a question regarding a web application that I am creating, where I have a REST service receiving a json string.

The Json line looks something like this:

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

I use resteasy to parse a json string that uses the jackson below it. I have an annotated jaxb class and I want to fully parse the "object" in a String variable. The reason I want to do this is the ability to parse json later using the correct parser (this depends on the application that sends the request, so it cannot be known in advance).

My annotated jaxb class is as follows:

 @XmlRootElement @XmlAccessorType(XmlAccessType.PROPERTY) public class Test{ @XmlElement(type = String.class) private String object; //getter and setter ... } 

When I make the remaining call and let Jenson parse this code, I get

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

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

Thanks in advance for any response.

+6
source share
4 answers

If I understand this question, you just want to use mechnanism, which converts Java-Object to JSON-String and vice versa.

I also need this while I used the connection to the WebSocket Client-Server, where the JSON string was passed.

For this, I used GSON (see GSON ). There you have the opportunity to create a full JSON-String. Here is an 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); } 

You need to match your Class-Attributes-setter and JSON-Attribute names. For instance.

 { "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; } } 
+1
source

@KwintenP Try using json smart library .

Then you can just get the JSON object with:

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

What else, you can get a specific object inside a JSON object, it can be another object, an array, and convert it to String or manipulate how you want.

+1
source
 java.lang.String out of START_OBJECT token 

this means that the expected character after "object" is quotation marks " , but not the brackets { .

Expected json

 "object" : "my object" 

Actual json

 "object" : { ... 

=======
If you need json parsing, as in your example, then change your class. For instance.

 @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 ... } 
0
source

You can also do something similar;

 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 } 

}

0
source

All Articles