将json字符串转换为java对象?

发布于 2024-11-08 15:27:01 字数 1047 浏览 0 评论 0原文

我一直在寻找与将 JSON 字符串转换为 Java 对象相关的示例,但没有找到任何好的示例。我发现的那个非常基本,并且没有真正处理复杂的 JSON 字符串。

我正在制作一个应用程序,使用谷歌翻译 API 将字符串从英语翻译成不同的语言。谷歌对查询的响应是...以下文本采用 JSON 格式,

{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}} 

到目前为止我的方法是使用 GSON API,但是,我实际上被困在如何操作这个复杂的结果并创建 java 对象上?

我的java类是...

import com.google.gson.Gson;

public class JSONConverter {

private String traslatedText;

/**
 * Create an object of it self by manipulating json string
 * @param json type: String
 * @return String Translated text result from JSON responce
 */
public String getTranslation(String json){  
    Gson gson = new Gson();
    JSONConverter obj = gson.fromJson(json, JSONConverter.class);

    return obj.getTranslationForReturn();
}

/**
 * Method return a translation to a private call
 * @return String translation
 */
private String getTranslationForReturn(){
    return this.traslatedText;
 }
}

以上方法不起作用,因为我没有得到“Bonjour tout le monde”返回,

如果有人可以扩展我的理解,那将是非常高兴的。

I have been looking around for examples related to converting JSON strings to Java object but haven't found any good examples. The one I found was really basic once and not really dealing with complex JSON strings.

I am making an app to translate strings from english to different languages using google translate api. Google's response upon query is...foolowing text is formatted in JSON,

{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}} 

my approach so far is using GSON API, however, I am stuck by actually how should I manipulate this complicated result and create java object?

My java class is...

import com.google.gson.Gson;

public class JSONConverter {

private String traslatedText;

/**
 * Create an object of it self by manipulating json string
 * @param json type: String
 * @return String Translated text result from JSON responce
 */
public String getTranslation(String json){  
    Gson gson = new Gson();
    JSONConverter obj = gson.fromJson(json, JSONConverter.class);

    return obj.getTranslationForReturn();
}

/**
 * Method return a translation to a private call
 * @return String translation
 */
private String getTranslationForReturn(){
    return this.traslatedText;
 }
}

Above approach is not working since I am not getting "Bonjour tout le monde" on return,

it would be a great pleasure if someone can extend my understanding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠魁 2024-11-15 15:27:01

编辑:
您需要您的 java 数据类是 JSON 的精确模型。所以你

{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}} 

会变成:

class DataWrapper {
    public Data data;

    public static DataWrapper fromJson(String s) {
        return new Gson().fromJson(s, DataWrapper.class);
    }
    public String toString() {
        return new Gson().toJson(this);
    }
}
class Data {
    public List<Translation> translations;
}
class Translation { 
    public String translatedText;
}

随着对象模型变得更加复杂,org.json 样式代码变得不可读,而 gson/jackson 样式对象映射仍然只是普通的 java 对象。

EDIT:
You need your java Data class to be an exact model of the JSON. So your

{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}} 

becomes:

class DataWrapper {
    public Data data;

    public static DataWrapper fromJson(String s) {
        return new Gson().fromJson(s, DataWrapper.class);
    }
    public String toString() {
        return new Gson().toJson(this);
    }
}
class Data {
    public List<Translation> translations;
}
class Translation { 
    public String translatedText;
}

As the object model gets more complicated the org.json style code veers towards unreadable whereas the gson/jackson style object mapping is still just plain java objects.

傲性难收 2024-11-15 15:27:01

看看这个 Converting JSON to Java ,我想你可以将一个类定义为对象并将其传递出去。因此,创建一些类并传递它。

Data obj = gson.fromJson(json, Data.class);

class Data{
  public List<Data> getTranslation(){
     return translations;
  }
}

尝试类似的方法。您可能需要进行一些尝试。

check this out Converting JSON to Java , i guess you can define a class as the object and pass that along. So, create some class and pass it along.

Data obj = gson.fromJson(json, Data.class);

class Data{
  public List<Data> getTranslation(){
     return translations;
  }
}

Try out something like that. You may need to experiment a little.

小巷里的女流氓 2024-11-15 15:27:01

我通常喜欢使用简单的 org.json 库

将其用作:

 // make sure the quotes are escaped
String str = "{\"data\":{\"translations\":[{\"translatedText\":\"Bonjour tout le monde\"}]}}";
 // the object data is inside a "global" JSONObject
data = new JSONObject(str).getJSONObject("data");
// get a JSONArray from inside an object
JSONArray translations = data.getJSONArray("translations");
// get the first JSONObject from the array
JSONObject text = translations.getJSONObject(0);
// get the String contained in the object -> logs Bonjour tout le monde
Log.d("***>Text", text.getString("translatedText"));

也许有这么多代码没有我想要的那么清楚。这是我会做的功能:

public String getTranslation(String json){  
  JSONObject data = new JSONObject(json).getJSONObject("data");
  return data.getJSONArray("translations").getJSONObject(0).getString("translatedText");
}

I usually like to use the plain org.json library

Use it as:

 // make sure the quotes are escaped
String str = "{\"data\":{\"translations\":[{\"translatedText\":\"Bonjour tout le monde\"}]}}";
 // the object data is inside a "global" JSONObject
data = new JSONObject(str).getJSONObject("data");
// get a JSONArray from inside an object
JSONArray translations = data.getJSONArray("translations");
// get the first JSONObject from the array
JSONObject text = translations.getJSONObject(0);
// get the String contained in the object -> logs Bonjour tout le monde
Log.d("***>Text", text.getString("translatedText"));

Maybe so much code is less clear than what I would have liked. This is the function as I would do it:

public String getTranslation(String json){  
  JSONObject data = new JSONObject(json).getJSONObject("data");
  return data.getJSONArray("translations").getJSONObject(0).getString("translatedText");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文