将json对象解析为字符串 [英] Parsing json object into a string

查看:438
本文介绍了将json对象解析为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于我正在建立的网络应用程序的问题,我有一个接收json字符串的REST服务。

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

Json字符串类似于:

The Json string is something like:

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

我正在使用resteasy来解析使用下面的jackson的json字符串。我有一个jaxb注释类,我想将对象完全解析为String变量。我想这样做的原因是以后能够使用正确的解析器解析json(它取决于发送请求的应用程序,所以事先无法知道)。

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).

我JAXB注解的类看起来像这样:

My jaxb annotated class looks like this:

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

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

//getter and setter
...
}



'p>当我执行剩下的电话,让杰克逊解析这个代码,我得到一个

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

错误。所以实际上我正在尝试将一个json字符串(一个json对象)解析为一个String。我似乎找不到有类似问题的人。

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.

提前感谢您的回复。

推荐答案

@KwintenP尝试使用 json智能库

@KwintenP Try using the json smart library.

然后您可以先使用以下命令检索JSON对象:

You can then simply retrieve the JSON object first using:

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

此外,您可以检索JSON对象内的特定对象,可能是另一个对象,一个数组并将其转换为String或操纵您想要的方式。

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.

这篇关于将json对象解析为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆