org.json.jar包是java配置中一款必不可少的jar包,如果需要创建JSONObject或者是JSONArray对象,就需要下载此jar包来实现,也是必备文件。有从事java开发的朋友不要错过,欢迎下载!

c5714e8c52a59d2fcef98043f131d4c9.png

org.json包安装方法:

首先导入org.json下面的包

package com.sun;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

public class Test {

public static void main(String args[]) throws JSONException {

String jsonContent = "{'hello':world,'abc':'xyz'}";

JSONObject jsonObject = new JSONObject(jsonContent);

String str1 = jsonObject.getString("hello");

String str2 = jsonObject.getString("abc");

System.out.println(str1);

System.out.println(str2);

System.out.println("------------------");

jsonContent = "[{'hello':333,'abc':'false','xyz':{'a':1,'b':'ab'}},{'hello':555,'abc':'true','xyz':{'a':2,'b':'ba'}}]";

JSONArray jsonArray = new JSONArray(jsonContent);

for(int i=0;i

JSONObject jsonobject2=jsonArray.getJSONObject(i);

int value1=jsonobject2.getInt("hello");

boolean value2=jsonobject2.getBoolean("abc");

// String value3=jsonobject2.getString("xyz");

JSONObject jsonobject3=jsonobject2.getJSONObject("xyz");

int value4=jsonobject3.getInt("a");

String value5=jsonobject3.getString("b");

System.out.println(value1);

System.out.println(value2);

System.out.println(value4);

System.out.println(value5);

}

}

}

输出结果

world

xyz

------------------

333

false

1

ab

555

true

2

ba

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐