首页 > 茶余饭后 > Java解析JSON的方法

Java解析JSON的方法

2009年7月24日 root 发表评论 阅读评论
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
    }
}

json

分类: 茶余饭后 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.