首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用java运行JSON

如何使用java运行JSON
EN

Stack Overflow用户
提问于 2012-07-02 00:27:37
回答 3查看 17.7K关注 0票数 1

几天前,我开始学习json。我有一段json代码,但我不能运行这段代码。请指导我该怎么做!!我应该在哪里以及如何运行这段代码?

代码语言:javascript
运行
复制
{
"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
    }
]
}
EN

回答 3

Stack Overflow用户

发布于 2012-07-02 00:51:53

看看这个http://www.json.org/java/

JSon是类似于XML的数据。但是没有结束标记。因此,您将指向该文件,让其中一个json解析器查看该文件,并调用函数来获取每个属性中的数据。如果您在使用json库时遇到问题,请发布您尝试使用的代码,我们将帮助您解决问题。

http://code.google.com/p/json-simple/

这是一个非常简单的json库(双关语),我认为这是最容易上手的,如果你对它有任何问题,请告诉我们。

票数 1
EN

Stack Overflow用户

发布于 2012-07-02 23:28:52

注意:我是的负责人,也是专家组的成员。

域模型

您需要某种数据结构来表示JSON数据,以便在Java应用程序中使用它。下面是一个使用类型化领域模型的示例。其他JSON库提供了更通用的类似DOM的结构。

Search

代码语言:javascript
运行
复制
package forum11283724;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Search {

    String title;
    List<Book> rows;

}

图书

代码语言:javascript
运行
复制
package forum11283724;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
    String title;
    String description;
    String imageHref;
}

演示代码

下面的代码示例将JSON文档读入Java对象,然后将它们写回JSON。当数据以对象形式存在时,您可以对其执行以下操作:添加、删除、修改等。

演示

代码语言:javascript
运行
复制
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

下面是您的JSON文档,它是本例的输入/输出

代码语言:javascript
运行
复制
{
   "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" : "É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."
   } ]
}
票数 1
EN

Stack Overflow用户

发布于 2012-07-02 00:43:15

一旦您有了json格式的数据,您就可以将其用作对某些请求的响应,并需要使用javascript循环遍历它才能将其显示给客户端(浏览器)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11283724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档