How to parse JSON in Java
来源:互联网 发布:linux lspci 插槽 编辑:程序博客网 时间:2024/06/04 23:30
From: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/
How to Parse JSON in Java
Submitted by tmo9d
Posted Sep 23 2009 12:25 PM
Posted Sep 23 2009 12:25 PM
Use json-lib, a library which adds JSON support to any Java program. json-lib can take a String and turn it into a JSONObject which can then be used to retrieve specific attributes.
1. Add this dependency to your project:
What does this mean? See How to Add a Dependency to a Java Project
2. Put the following JSON sample in your classpath:
3. Load the resource from the classpath and parse this JSON as follows:
Note that JSONSerializer returns a JSON object. This is a general object which could be a JSONObject or a JSONArray depending on the JSON you are trying to parse. In this example, since I know that the JSON is a JSONObject, I can cast the result directly to a JSONObject. If you are dealing with JSON that could return a JSONArray, you'll likely want to check the type of the object that is returned by toJSON.
This sample project is available on GitHub here: http://github.com/to...le-json-parsing
For more information about the json-lib project, see JSON-LIB project page
1. Add this dependency to your project:
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.3</version> <scope>compile</scope> </dependency>
What does this mean? See How to Add a Dependency to a Java Project
2. Put the following JSON sample in your classpath:
{'foo':'bar', 'coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz', 'lastName':'Aldrin'}, 'mission':'apollo 11'}
3. Load the resource from the classpath and parse this JSON as follows:
package com.discursive.answers;import java.io.InputStream;import net.sf.json.JSONObject;import net.sf.json.JSONSerializer;import org.apache.commons.io.IOUtils;public class JsonParsing { public static void main(String[] args) throws Exception { InputStream is = JsonParsing.class.getResourceAsStream( "sample-json.txt"); String jsonTxt = IOUtils.toString( is ); JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt ); double coolness = json.getDouble( "coolness" ); int altitude = json.getInt( "altitude" ); JSONObject pilot = json.getJSONObject("pilot"); String firstName = pilot.getString("firstName"); String lastName = pilot.getString("lastName"); System.out.println( "Coolness: " + coolness ); System.out.println( "Altitude: " + altitude ); System.out.println( "Pilot: " + lastName ); }}
Note that JSONSerializer returns a JSON object. This is a general object which could be a JSONObject or a JSONArray depending on the JSON you are trying to parse. In this example, since I know that the JSON is a JSONObject, I can cast the result directly to a JSONObject. If you are dealing with JSON that could return a JSONArray, you'll likely want to check the type of the object that is returned by toJSON.
This sample project is available on GitHub here: http://github.com/to...le-json-parsing
For more information about the json-lib project, see JSON-LIB project page
Learn more about this topic from Java Web Services: Up and Running.
This quick, practical, and thorough introduction to Java web services -- the JAX-WS and JAX-RS APIs -- offers a mix of architectural overview, complete working code examples, and short yet precise instructions for compiling, deploying, and executing a sample application. You'll not only learn how to write web services from scratch, but also how to integrate existing services into your Java applications.
Tags:
- java
- json
0
2 Replies
Alternative solution by Stuart12345
Posted Sep 30 2011 10:13 AM
Posted Sep 30 2011 10:13 AM
Be sure to add the classifier:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
see http://stackoverflow...net-sf-json-lib
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
see http://stackoverflow...net-sf-json-lib
Alternative solution by Adam Musial-Bright
Posted Oct 03 2011 09:39 AM
Posted Oct 03 2011 09:39 AM
Gson, the Google JSON library is also very nice.
You will need a simple POJOs for the JSON representation:
Now you can convert a JSON representation into a JSON object:
done.
You will need a simple POJOs for the JSON representation:
// JSON representationclass MyJson { public MyJson() {} private String foo; private float coolness; // other attributes public void setFoo(String foo) { this.foo = foo; } public String getFoo() { return foo; } public void setCoolness(float coolness) { this.coolness = coolness; } public float getCoolness() { return coolness; }}
Now you can convert a JSON representation into a JSON object:
Gson gson = new Gson(); MyJson myJson = new gson.fromJson(jsonTxt, MyJson.class); // now you have a real java object System.out.println(myJson.getFoo());
done.
- How to parse JSON in Java
- How to parse JSON
- How to parse the overloading methods in Java 5.0
- How to read and parse CSV file in Java
- How to Parse or Read XML File in Java >> XML Tutorial Example
- How to read and write JSON files in Java(Gson)
- How to parse / read JSON data into a Android ListView
- How to parse a URI like this in Java http://translate.google.com/#zh-CN|en|你
- How to get Json in Android
- how to pase JSON in Swift
- parse json into string from http response in java
- How to use json with as3corelib in flex( JSON )
- How to convert XML to JSON in ASP.NET C#
- How to convert XML to JSON in ASP.NET C#
- java 解析(parse) json
- Parse string to Json object
- how to install java in ubuntu
- How to execute shell script in Java?
- Android学习笔记之ImageSwitcher
- 用Facebook账号登陆Pinterest的用户97%都为女性
- AIX 启动
- Linux系统资源监控命令vmstat\mpstat\sar\iostat\top
- 0CORDMODE如何控制上传数据
- How to parse JSON in Java
- 感谢CSDN 及CSDN所有上传资源的作者
- InfoCube的specific properties
- 使用ajax后后台调用js函数
- fedora 文本方式启动
- reiser4 和xfs 测试比较
- 嵌入式Linux开发培训资料整理
- 如何进行有效沟通
- Java中的volatile和synchronized