JSON参数构造及如何获取

来源:互联网 发布:linux排序命令 编辑:程序博客网 时间:2024/06/05 19:07

JSON格式:

{newProcessId:'',newActDefUniqueId:''}//key可以不加引号,value必须有

构造方法:
 
   JSONObject obj = new JSONObject();   obj.put( "name" , "feiniu5566");   obj.put( "age" ,23);   out.print(obj.toString());

举例:

{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },],  "authors": [{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },] }JsonObject obj=new JsonObject();JsonObject obj2=new JsonObject();JsonObject obj3=new JsonObject();obj2.put("firstName","Brett");obj2.put("lastName","McLaughlin");obj2.put("email","aaaa");obj3.put("firstName","Isaac");obj3.put("lastName","Asimov");obj3.put("genre","science fiction");obj.put("programmers",obj2);obj.put("authors",obj3);out.print(obj.toString());

获取JSON:

Js中

var json=eval(param);var newProcessId=json.newProcessId;var newActDefUniqueId=json.newActDefUniqueId;

Java中

import org.json.JSONArray;import org.json.JSONException; public class test2 {    public static void main(String[] args) throws JSONException {        String str = "[{'columnId':5,'columnName':'人文历史'},{'columnId':2,'columnName':'商业视野'}]}";        JSONArray jsonArray = null;        jsonArray = new JSONArray(str);        System.out.println(jsonArray.getJSONObject(0).get("columnName"));    }}


0 0
原创粉丝点击