java封装json串示例_Java JSON示例

java封装json串示例

In this tutorial you will learn about Java JSON example.

在本教程中,您将学习Java JSON示例。

What is JSON?

什么是JSON?

  • JSON stands for JavaScript Object Notation.

    JSON代表JavaScript对象符号。

  • JSON is a lightweight and an easier way for exchanging data on the web.

    JSON是一种轻量级且更容易在网络上交换数据的方法。

  • JSON is originated from JavaScript programming language.

    JSON源自JavaScript编程语言。

  • JSON is a good alternative of XML.

    JSON是XML的很好替代。

  • JSON is language independent and supports data structures like object and array.

    JSON与语言无关,并且支持对象和数组之类的数据结构。

Java JSON Example

Java JSON示例 (Java JSON Example)

To use JSON with Java we require a library called as json.simple. You can download it from below link.

要将JSON与Java结合使用,我们需要一个名为json.simple的库。 您可以从下面的链接下载它。

Download: http://www.javatpoint.com/jsonpages/json-simple-1.1.1.jar

下载: http : //www.javatpoint.com/jsonpages/json-simple-1.1.1.jar

After downloading it you need to import it to the IDE (NetBeans, Eclipse, etc) you are using. 

下载后,您需要将其导入到您正在使用的IDE(NetBeans,Eclipse等)中。

用Java编码和解码JSON对象 (Encode and Decode JSON object in Java)

JSON object contains data in the form of key and value pair. A JSON object example is given below.

JSON对象包含键和值对形式的数据。 下面给出一个JSON对象示例。

{"name":"Neeraj Mishra","age":21}

An example to encode and decode JSON object in Java is given below.

下面给出了使用Java编码和解码JSON对象的示例。

import org.json.simple.JSONObject;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		
		//creating JSON object
		JSONObject obj=new JSONObject();
		
		//Encode JSON Object
		obj.put("name","Neeraj Mishra");
		obj.put("age",new Integer(21));
		
		//Decode JSON Object
		System.out.println("Name:"+obj.get("name"));
		System.out.println("Age:"+obj.get("age"));
	}
}

Output

输出量

Neeraj Mishra 21

Neeraj Mishra 21

用Java编码和解码JSON数组 (Encode and Decode JSON Array in Java)

A JSON array example is given below.

JSON数组示例如下。

["C","C++","Java","Python"]

An example to encode and decode JSON array in Java is given below.

下面给出了使用Java编码和解码JSON数组的示例。

import org.json.simple.JSONArray;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		
		//creating JSON Array
		JSONArray ar=new JSONArray();
		
		//Encode JSON Array
		ar.add("C");
		ar.add("C++");
		ar.add("Java");
		ar.add("Python");
		
		//Decode JSON Array
		for(int i=0;i<ar.size();++i) {
			System.out.println(ar.get(i));			
		}
	}
}

Output

输出量

C C++ Java Python

C C ++ Java Python

用Java编码和解码JSON对象数组 (Encode and Decode JSON Array of Objects in Java)

A JSON array of objects example is given below.

以下是对象的JSON数组示例。

[{"name":"C"},{"name":"C++"},{"name":"Java"}]

An example to encode and decode JSON array of objects in Java is given below.

下面给出了一个用Java编码和解码对象的JSON数组的示例。

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		
		//creating JSON Array
		JSONArray ar=new JSONArray();
 
		JSONObject obj;
		
		//Creating and adding first JSON Object to JSON Array
		obj=new JSONObject();
		obj.put("name","C");
		ar.add(obj);
		
		//Creating and adding second JSON Object to JSON Array
		obj=new JSONObject();
		obj.put("name","C++");
		ar.add(obj);
 
		//Creating and adding third JSON Object to JSON Array
		obj=new JSONObject();
		obj.put("name","Java");
		ar.add(obj);
 
		
		//Retrieving JSON Objects from JSON Array
		for(int i=0;i<ar.size();++i) {
			obj=(JSONObject)ar.get(i);
			System.out.println(obj.get("name"));			
		}
	}
}

Output

输出量

C C++ Java

C C ++ Java

用Java解码JSON字符串 (Decode JSON String in Java)

We can decode JSON string in following way.

我们可以通过以下方式解码JSON字符串。

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		String JSONObjectString="{\"name\":\"Neeraj Mishra\"}";
		String JSONArrayString="[\"C\",\"C++\",\"Java\"]";
 
		JSONObject obj=(JSONObject)JSONValue.parse(JSONObjectString);
		JSONArray ar=(JSONArray)JSONValue.parse(JSONArrayString);
		
		System.out.println(obj);
		System.out.println(ar);
 
	}
}

Output

输出量

{“name”:”Neeraj Mishra”} [“C”,”C++”,”Java”]

{“名称”:“ Neeraj Mishra”} [“ C”,“ C ++”,“ Java”]

The above Java JSON example is self explanatory, still if you are unable to understand then feel free to ask by commenting below.

上面的Java JSON示例是不言自明的,如果您仍然无法理解,请随时在下面的评论中提问。

翻译自: https://www.thecrazyprogrammer.com/2015/12/java-json-example.html

java封装json串示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值