1

数日前、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
    }
]
}
4

3 に答える 3

1

このhttp://www.json.org/java/を見てください。

JSonはXMLに類似したデータです。ただし、終了タグはありません。したがって、そのファイルをポイントし、jsonパーサーの1つにそのファイルを調べてもらい、関数を呼び出して各属性内のデータを取得します。jsonライブラリのいずれかを使用する際に問題が発生した場合は、使用しようとしているコードを投稿してください。サポートさせていただきます。

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

これは本当に単純なjsonライブラリです(しゃれを意図しています)。始めるのが最も簡単だと思います。問題が発生した場合はお知らせください。

于 2012-07-01T16:51:53.307 に答える
1

注: 私はEclipseLink JAXB (MOXy)のリーダーであり、JAXB (JSR-222)エキスパート グループのメンバーです。

ドメインモデル

Java アプリケーションで JSON データを操作できるように、何らかのデータ構造で JSON データを表現する必要があります。以下は、型付きドメイン モデルの使用例です。他の JSON ライブラリは、より一般的な DOM のような構造を提供します。

探す

package forum11283724;

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

@XmlAccessorType(XmlAccessType.FIELD)
public class Search {

    String title;
    List<Book> rows;

}

package forum11283724;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
    String title;
    String description;
    String imageHref;
}

デモコード

以下は、JSON ドキュメントを Java オブジェクトに読み取り、それらを JSON に書き戻すコード サンプルです。データがオブジェクト形式の場合、追加、削除、変更などのアクションを実行できます。

デモ

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);
    }

}

入出力

以下は、この例の入力/出力である 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" : "É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."
   } ]
}
于 2012-07-02T15:28:52.037 に答える
0

json 形式のデータを取得したら、それをリクエストの一部に対する応答として使用し、それをクライアント (ブラウザー) に表示するには、javascript for loop を使用してループする必要があります。

于 2012-07-01T16:43:14.980 に答える