簡體   English   中英

如何將JSON字符串轉換為GSON對象?

[英]How do I convert a JSON String into a GSON Object?

我將開發一個web服務,它的輸入應該用JSON描述,我希望在我的內部邏輯中用作GSON對象。 因此,無論誰使用Web服務,都會發送一些請求,其中包含以下信息:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
 }
}

我現在需要的是處理它的東西。 類似於以下內容:(這只是一個例子,我想象以下步驟讓它運行;))

public class AnyClass{

    public AnyClass(String jsonString){
        GSONObject gobject = new GSONObject(jsonString);
        String title = gobject.getValueOf("title");
    }
}

謝謝你的每一個幫助:)

嘗試這個

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(jsonString);

用gson

Gson gson = new Gson();
gson.fromJson(jsonString, YourBean.class);

您可以使用javax.json包來處理json數據。 有關詳細信息,請參閱https://docs.oracle.com/javaee/7/tutorial/jsonp002.htm

將json字符串轉換為Gson(反序列化)

使用gson.fromJson();

使用String類的示例

Gson gson = new Gson();
String str = gson.fromJson("\"abc\"", String.class);

使用用戶定義的類的示例

BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);

Gson的谷歌幫助頁面有很好的解釋。

https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples

對象示例

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

(串行化)

BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
==> json is {"value1":1,"value2":"abc"}

請注意,您無法使用循環引用序列化對象,因為這將導致無限遞歸。

(反序列化)

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   
==> obj2 is just like obj

更精細的對象點

It is perfectly fine (and recommended) to use private fields 
There is no need to use any annotations to indicate a field is to be included for serialization and deserialization. All fields in the current class (and from all super classes) are included by default. 
If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.

This implementation handles nulls correctly

While serialization, a null field is skipped from the output

While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null

If a field is synthetic, it is ignored and not included in JSON serialization or deserialization

Fields corresponding to the outer classes in  inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization

如果您需要更多示例,請查看以下鏈接

http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

http://filotechnologia.blogspot.it/2013/09/convert-java-object-from-json-gson-api.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM