Java中如何操作JSON格式的字符串

1. 首先需要一个类支持 JSONObject类,该类需要外部JAR包支持,下面是下载地址

  jettison-1.0.jar包下载地址:http://www.kuaipan.cn/file/id_21487635482542624.htm

 

2. 下面在代码中演示如何用

 1 import org.codehaus.jettison.json.JSONException;

 2 import org.codehaus.jettison.json.JSONObject;

 3 

 4 public class HelloWorldTest {

 5     public static void main(String[] args) {

 6         String jsonStr = "{\"b_action\":\"xts.run\",\"m\":\"portal/report-viewer.xts\",\"method\":\"execute\","

 7                 + "\"ui.action\":\"run\",\"ui.tool\":\"CognosViewer\",\"ui.object\":\"/content/package[@name='zjfx_bdgc']/report[@name='a']\","

 8                 + "\"ZTBM\":\"000010000200001000010000100001\",\"MBBM\":\"001\",\"p_d\":\"2011010000100001\","

 9                 + "\"run.prompt\":\"false\",\"ZBYQ_DJ\":\"1111\",\"BDGC_DYDJ\":\"22222\",\"BDGC_JSXZ\":\"01\","

10                 + "\"JSZB_BDZXS\":\"01,02,03\",\"PDXS_GYC_PDZZXH\":\"02\",\"ZBYQ_BQTS1\":\"1\",\"ZBYQ_BQTS2\":\"2\","

11                 + "\"ZBYQ_DTRL1\":\"3\",\"ZBYQ_DTRL2\":\"4\",\"JGJS_JTTZ1\":\"5\","

12                 + "\"JGJS_JTTZ2\":\"6\",\"btnsearch\":\"查询\",\"btnreset\":\"重置\"}";

13         

14         try {

15             JSONObject obj = new JSONObject(jsonStr);

16             String b_action = (String)obj.get("b_action");

17             System.out.println(b_action);

18         } catch (JSONException e) {

19             e.printStackTrace();

20         }

21     }

22 }

 

 

你可能感兴趣的:(java)