怎样将webservice服务发出的数据转为Json格式的

我刚入职不久,求大神指教。以下是我写的代码,但是编译出错了。比较我急,求大侠帮助

import java.util.HashMap;
import java.util.Map;

//import org.json.*;
import org.json.JSONException;
import org.json.JSONObject;

public class test {

public static void main(String[] args) {
    String json = "{\"name\":\"reiz\"}";
    JSONObject jsonObj = null;

// try {
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);

// } catch (Exception e) {
// e.printStackTrace();
// }
Map ingredients = new HashMap();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
try {
jsonObj.put("ingredients", ingredients);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(jsonObj);

    System.out.println(jsonObj);
    System.out.print(json);

错误在这句话:
JSONObject jsonObj = new JSONObject(json);

还有我写了一个服务 为什么发布不了啊
public void doService(HttpServletRequest request, HttpServletResponse response) throws IOException {

String s1 = request.getParameter("username");

String s2 = request.getParameter("passwd");
// JSONObject jsonObj = new JSONObject(s3);

try {

JSONObject jsonObj1 = new JSONObject(s1);

JSONObject jsonObj2= new JSONObject(s2);
System.out.println(jsonObj1.getString("model"));

System.out.println(jsonObj2.getInt("year"));

response.getWriter().print(jsonObj1.toString());

response.getWriter().print(jsonObj2.toString());
} catch (JSONException e) {
e.printStackTrace();
}
// response.getWriter().print("{ \"name\": \"Violet\", \"occupation\": \"character\" }");
// response.getWriter().print(jsonObj.toString());

}
如果去掉这个服务就可以发布。

你用的是哪个JSON库?

编译不通过都不能解决啊?看提示啊,提示说什么问题编译不通过的?

  1. 关于编译不通过,很明显, JSONObject 没有这样的构造函数。

JSONObject jsonObj = new JSONObject(json);
改成:
JSONObject jsonObj = new JSONObject();
jsonObj.put("name","reiz");

删掉这行:
String json = "{\"name\":\"reiz\"}";

  1. 关于那个服务,你是用的什么 WebService 运行环境啊? 咋一看,完全就是 Servlet 里面的 doPost/doGet 的样子?难道你以为在 Servlet 里写个 doService 这样就是写了一个 WebService ? public void doService(HttpServletRequest request, HttpServletResponse response) throws IOException { ... }

JSONObject 的完整qualified name贴出来看看