Home  >  Q&A  >  body text

java - JSon字符串转JSON对象?

String userStr=readJSONString(request);

        System.out.println(userStr);//能输出
        JSONObject jsonObj = JSONObject.fromObject(userStr);//我感觉这有问题

        System.out.println(jsonObj.getInt("userid"));//后台没输出
        System.out.println(jsonObj.getString("username"));
        输出结果如下

大家讲道理大家讲道理2743 days ago472

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:54:09

    should be JSONObject import错了,或者是版本不对吧,我本地用的是 json-lib-2.4 net.sf.json.JSONObject, which is OK.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:54:09

    If the frontend transmits json type data, then the backend receives json type data, and there is no need to transfer it anymore

    reply
    0
  • PHPz

    PHPz2017-04-18 10:54:09

    1. First confirm whether your userStr is a json string:
    For example: String jstr="{'json':'jsonvalue','bool':true,'int':1,'double':'20.5'} ";
    2. Then check whether the JOSNObject you are using is the json.jar package.
    3. Confirm whether your json package is imported correctly.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:54:09

    First take a look at the package introduction of the net.sf.json.JSONObject version mentioned by taomaokun, and then check whether your json string is written correctly. This is generally available

    reply
    0
  • Cancelreply