使用Java操作JSON字符串对象

在Java中解析与构造JSON[转]

www.json.org上公布了很多Java下的json解析工具,其中org.json和json-lib比较简单,两者使用上差不多。下面两段源代码是分别使用这两个工具解析和构造JSON的演示程序。

http://nchc.dl.sourceforge.net/sourceforge/json-lib/json-lib-2.2.1-jdk15.jar
(Needs libs below:)
[

  • jakarta commons-lang 2.3
  • jakarta commons-beanutils 1.7.0
  • jakarta commons-collections 3.2
  • jakarta commons-logging 1.1
  • ezmorph 1.0.4

]
这是使用json-lib的程序:
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
public class Test {
     public static void main(String[] args) {
         String json = "{/"name/":/"reiz/"}";
         JSONObject jsonObj = JSONObject.fromObject(json);
         String name = jsonObj.getString("name");
         jsonObj.put("initial", name.substring(0, 1).toUpperCase());
         String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
         jsonObj.put("likes", likes);
         Map <String, String> ingredients = new HashMap <String, String>();
         ingredients.put("apples", "3kg");
         ingredients.put("sugar", "1kg");
         ingredients.put("pastry", "2.4kg");
         ingredients.put("bestEaten", "outdoors");
         jsonObj.put("ingredients",ingredients);
         System.out.println(jsonObj);
     }
}

http://www.json.org/java/json.zip
这是使用org.json的程序:
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
     public static void main(String[] args) throws JSONException {
         String json = "{/"name/":/"reiz/"}";
         JSONObject jsonObj = new JSONObject(json);
         String name = jsonObj.getString("name");
         jsonObj.put("initial", name.substring(0, 1).toUpperCase());
         String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
         jsonObj.put("likes", likes);
         Map <String, String> ingredients = new HashMap <String, String>();
         ingredients.put("apples", "3kg");
         ingredients.put("sugar", "1kg");
         ingredients.put("pastry", "2.4kg");
         ingredients.put("bestEaten", "outdoors");
         jsonObj.put("ingredients", ingredients);
         System.out.println(jsonObj);
         System.out.println(jsonObj);
     }
}
两者的使用几乎是相同的,但org.json比json-lib要轻量得多,前者没有任何依赖,而后者要依赖ezmorph和commons的lang、logging、beanutils、collections等组件。

 

=======================================================================

使用Java操作JSON字符串对象

Posted by eDWARD at 10:14

  1. 如果我们需要实现一个配置管理的功能,那么为每个配置项目增加一个字段既复杂也不利于扩展,所以我们通常使用一个字符串来保存配置项目信息,这里介绍如何使用json的字符串解析来达到刚才说的目的。引入Json需要的类库:
         
         
    1. import org.json.JSONException;   
    2. import org.json.JSONObject;  
  2. 生成一个json对象(可以添加不同类型的数据):
         
         
    1. JSONObject jsonObject = new JSONObject();   
    2. jsonObject.put("a"1);   
    3. jsonObject.put("b"1.1);   
    4. jsonObject.put("c", 1L);   
    5. jsonObject.put("d""test");   
    6. jsonObject.put("e"true);   
    7. System.out.println(jsonObject);   
    8. //{"d":"test","e":true,"b":1.1,"c":1,"a":1}  
  3. 解析一个json对象(可以解析不同类型的数据):
         
         
    1. jsonObject = getJSONObject("{d:test,e:true,b:1.1,c:1,a:1}");   
    2. System.out.println(jsonObject);   
    3. //{"d":"test","e":true,"b":1.1,"c":1,"a":1}   
    4. System.out.println(jsonObject.getInt("a"));   
    5. System.out.println(jsonObject.getDouble("b"));   
    6. System.out.println(jsonObject.getLong("c"));   
    7. System.out.println(jsonObject.getString("d"));   
    8. System.out.println(jsonObject.getBoolean("e"));  

    getJSONObject(String str)

         
         
    1. public static JSONObject getJSONObject(String str) {   
    2.   if (str == null || str.trim().length() == 0)   
    3.     return null;   
    4.   JSONObject jsonObject = null;   
    5.   try {   
    6.     jsonObject = new JSONObject(str);   
    7.   } catch (JSONException e) {   
    8.     e.printStackTrace(System.err);   
    9.   }   
    10.   return jsonObject;   
    11. }  

    这样我们不仅可以处理多种数据类型,还可以随时添加配置相,这种方式相当灵活。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值