如何使用 Java 运行 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run JSON using java
提问by Rajat Talwar
few days ago i've started studing on json. i've got a code of json but i m unabe to run this code. please guide me what should i do !! where and how should i run this code?
几天前,我开始研究 json。我有一个 json 代码,但我无法运行此代码。请指导我我该怎么做!!我应该在哪里以及如何运行此代码?
{
"title":"About Canada",
"rows":[
{
"title":"Beavers",
"description":"Beavers are second only to humans in their ability to manipulate and change their environment. They can measure up to 1.3 metres long. A group of beavers is called a colony",
"imageHref":"http://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/American_Beaver.jpg/220px-American_Beaver.jpg"
},
{
"title":"Flag",
"description":null,
"imageHref":"http://images.findicons.com/files/icons/662/world_flag/128/flag_of_canada.png"
},
{
"title":"Transportation",
"description":"It is a well known fact that polar bears are the main mode of transportation in Canada. They consume far less gas and have the added benefit of being difficult to steal.",
"imageHref":"http://1.bp.blogspot.com/_VZVOmYVm68Q/SMkzZzkGXKI/AAAAAAAAADQ/U89miaCkcyo/s400/the_golden_compass_still.jpg"
},
{
"title":"Hockey Night in Canada",
"description":"These Saturday night CBC broadcasts originally aired on radio in 1931. In 1952 they debuted on television and continue to unite (and divide) the nation each week.",
"imageHref":"http://fyimusic.ca/wp-content/uploads/2008/06/hockey-night-in-canada.thumbnail.jpg"
},
{
"title":"Eh",
"description":"A chiefly Canadian interrogative utterance, usually expressing surprise or doubt or seeking confirmation.",
"imageHref":null
},
{
"title":"Housing",
"description":"Warmer than you might think.",
"imageHref":"http://icons.iconarchive.com/icons/iconshock/alaska/256/Igloo-icon.png"
},
{
"title":"Public Shame",
"description":" Sadly it's true.",
"imageHref":"http://static.guim.co.uk/sys-images/Music/Pix/site_furniture/2007/04/19/avril_lavigne.jpg"
},
{
"title":null,
"description":null,
"imageHref":null
},
{
"title":"Space Program",
"description":"Canada hopes to soon launch a man to the moon.",
"imageHref":"http://files.turbosquid.com/Preview/Content_2009_07_14__10_25_15/trebucheta.jpgdf3f3bf4-935d-40ff-84b2-6ce718a327a9Larger.jpg"
},
{
"title":"Meese",
"description":"A moose is a common sight in Canada. Tall and majestic, they represent many of the values which Canadians imagine that they possess. They grow up to 2.7 metres long and can weigh over 700 kg. They swim at 10 km/h. Moose antlers weigh roughly 20 kg. The plural of moose is actually 'meese', despite what most dictionaries, encyclopedias, and experts will tell you.",
"imageHref":"http://caroldeckerwildlifeartstudio.net/wp-content/uploads/2011/04/IMG_2418%20majestic%20moose%201%20copy%20(Small)-96x96.jpg"
},
{
"title":"Geography",
"description":"It's really big.",
"imageHref":null
},
{
"title":"Kittens...",
"description":"éare illegal. Cats are fine.",
"imageHref":"http://www.donegalhimalayans.com/images/That%20fish%20was%20this%20big.jpg"
},
{
"title":"Mounties",
"description":"They are the law. They are also Canada's foreign espionage service. Subtle.",
"imageHref":"http://3.bp.blogspot.com/__mokxbTmuJM/RnWuJ6cE9cI/AAAAAAAAATw/6z3m3w9JDiU/s400/019843_31.jpg"
},
{
"title":"Language",
"description":"Nous parlons tous les langues importants.",
"imageHref":null
}
]
}
回答by Frank Visaggio
Take a look at this http://www.json.org/java/.
看看这个http://www.json.org/java/。
JSon is data similiar to XML . However without end tags . So you would point to that file and have one of the json parsers look at it and call functions to get the data inside each attribute . If you are having problems using one of the json libraries post up the code your trying to use and we will help you out .
JSon 是类似于 XML 的数据。但是没有结束标签。因此,您将指向该文件并让其中一个 json 解析器查看它并调用函数以获取每个属性中的数据。如果您在使用 json 库之一时遇到问题,请张贴您尝试使用的代码,我们将为您提供帮助。
http://code.google.com/p/json-simple/
http://code.google.com/p/json-simple/
That's a really simple json library ( pun intended ) I think the easiest to get started with , let us know if you have any trouble with it .
这是一个非常简单的 json 库(双关语)我认为最容易上手,如果您有任何问题,请告诉我们。
回答by bdoughan
Note:I'm the EclipseLink JAXB (MOXy)lead and a member of the JAXB (JSR-222)expert group.
注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222)专家组的成员。
DOMAIN MODEL
域模型
You will want some sort of data structure to represent your JSON data so that you can work with it in your Java application. Below is an example of using a typed domain model. Other JSON libraries offer a more generic DOM like structure.
您将需要某种数据结构来表示您的 JSON 数据,以便您可以在 Java 应用程序中使用它。下面是使用类型化域模型的示例。其他 JSON 库提供了更通用的类似 DOM 的结构。
Search
搜索
package forum11283724;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Search {
String title;
List<Book> rows;
}
Book
书
package forum11283724;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
String title;
String description;
String imageHref;
}
DEMO CODE
演示代码
Below is code sample that read the JSON document into Java objects, and then writes them back to JSON. When the data is in object form you could act on it: add, remove, modify, etc.
下面的代码示例将 JSON 文档读入 Java 对象,然后将它们写回 JSON。当数据为对象形式时,您可以对其进行操作:添加、删除、修改等。
Demo
演示
package forum11283724;
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String,Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Search.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource(new FileReader("src/forum11283724/input.json"));
Search search = (Search) unmarshaller.unmarshal(json, Search.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(search, System.out);
}
}
INPUT/OUTPUT
输入输出
Below is your JSON document which is the input/output for this example
以下是您的 JSON 文档,它是此示例的输入/输出
{
"title" : "About Canada",
"rows" : [ {
"title" : "Beavers",
"description" : "Beavers are second only to humans in their ability to manipulate and change their environment. They can measure up to 1.3 metres long. A group of beavers is called a colony",
"imageHref" : "http://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/American_Beaver.jpg/220px-American_Beaver.jpg"
}, {
"title" : "Flag",
"imageHref" : "http://images.findicons.com/files/icons/662/world_flag/128/flag_of_canada.png"
}, {
"title" : "Transportation",
"description" : "It is a well known fact that polar bears are the main mode of transportation in Canada. They consume far less gas and have the added benefit of being difficult to steal.",
"imageHref" : "http://1.bp.blogspot.com/_VZVOmYVm68Q/SMkzZzkGXKI/AAAAAAAAADQ/U89miaCkcyo/s400/the_golden_compass_still.jpg"
}, {
"title" : "Hockey Night in Canada",
"description" : "These Saturday night CBC broadcasts originally aired on radio in 1931. In 1952 they debuted on television and continue to unite (and divide) the nation each week.",
"imageHref" : "http://fyimusic.ca/wp-content/uploads/2008/06/hockey-night-in-canada.thumbnail.jpg"
}, {
"title" : "Eh",
"description" : "A chiefly Canadian interrogative utterance, usually expressing surprise or doubt or seeking confirmation."
}, {
"title" : "Housing",
"description" : "Warmer than you might think.",
"imageHref" : "http://icons.iconarchive.com/icons/iconshock/alaska/256/Igloo-icon.png"
}, {
"title" : "Public Shame",
"description" : " Sadly it's true.",
"imageHref" : "http://static.guim.co.uk/sys-images/Music/Pix/site_furniture/2007/04/19/avril_lavigne.jpg"
}, {
}, {
"title" : "Space Program",
"description" : "Canada hopes to soon launch a man to the moon.",
"imageHref" : "http://files.turbosquid.com/Preview/Content_2009_07_14__10_25_15/trebucheta.jpgdf3f3bf4-935d-40ff-84b2-6ce718a327a9Larger.jpg"
}, {
"title" : "Meese",
"description" : "A moose is a common sight in Canada. Tall and majestic, they represent many of the values which Canadians imagine that they possess. They grow up to 2.7 metres long and can weigh over 700 kg. They swim at 10 km/h. Moose antlers weigh roughly 20 kg. The plural of moose is actually 'meese', despite what most dictionaries, encyclopedias, and experts will tell you.",
"imageHref" : "http://caroldeckerwildlifeartstudio.net/wp-content/uploads/2011/04/IMG_2418%20majestic%20moose%201%20copy%20(Small)-96x96.jpg"
}, {
"title" : "Geography",
"description" : "It's really big."
}, {
"title" : "Kittens...",
"description" : "√aare illegal. Cats are fine.",
"imageHref" : "http://www.donegalhimalayans.com/images/That%20fish%20was%20this%20big.jpg"
}, {
"title" : "Mounties",
"description" : "They are the law. They are also Canada's foreign espionage service. Subtle.",
"imageHref" : "http://3.bp.blogspot.com/__mokxbTmuJM/RnWuJ6cE9cI/AAAAAAAAATw/6z3m3w9JDiU/s400/019843_31.jpg"
}, {
"title" : "Language",
"description" : "Nous parlons tous les langues importants."
} ]
}
回答by amitprot
Once You have the data in json format , you can use it as a response for some of your request and to show it to the client (browser) you need to loop through it using javascriptfor loop .
获得 json 格式的数据后,您可以将其用作某些请求的响应,并将其显示给客户端(浏览器),您需要使用javascriptfor loop遍历它。