English 中文(简体)
将这名JSON与 Java混为一谈?
原标题:Parsing this JSON with Java?
  • 时间:2011-10-05 21:12:10
  •  标签:
  • java
  • json

在假释时 我通常只是对物体进行校对,并使用<条码>gsonlibrary将我的“强令”插入该物体。

然而,我现在发现,我的反应相当复杂,包括各种各样物体和阵列的子元素。 ......

{
 "type": "thetype",
 "object":{
   "text": "texthere",
   "moretext": "more here"
  },
  ...,
  ...,
  ...,
  ...,
  "fieldIwant": [
   {
    "object":"object!"
    },
   ....
   ....
   {
    "object":"object!"
    },
   ]
}

事情是,我对<代码>fieldIwant没有任何其他意义。 在 Java,我没有办法只提这个领域,单独与它合作,而不是我不需要的所有其他致命的重量?

最佳回答

http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html?com/google/gson/JsonParser.html 目 录

JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
yourArray = new Gson().fromJson(jsonObject.get("fieldIwant"), yourArrayType);

Alternatively you can create an ExclusionStrategy to use with a GsonBuilder

问题回答

According to this http://sites.google.com/site/gson/gson-design-document it looks like gson does this for you by default.

当你将一座Json的座标降为一种理想的物体时,你可以浏览投入的树种,也可以浏览所希望的树种。 Gson采用后一种方法对目标物体的类型进行联系。 这使你能够严格控制,只看一看你所期望的物体类型(基本上确认对预期“希马”的投入)。 By 这样做,你也忽视了Json投入但尚未预期的任何额外领域。

换言之,它预示着你不需要的任何领域。 你们应当善待。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...