【问题标题】:JSON Object becomes key of another JSON objectJSON 对象成为另一个 JSON 对象的键
【发布时间】:2018-10-05 18:59:30
【问题描述】:

生成 JSON 对象并调用 Async 方法的代码:

final String OWNER_PASSWORD="ownerPassword";
final String OWNER_EMAIL="ownerEmailId";

    private void CheckPassword() {

    shopkeeperJSON = new JSONObject();
    try{
        shopkeeperJSON.put(OWNER_EMAIL,"varuncr7raj@gmail.com");
        shopkeeperJSON.put(OWNER_PASSWORD,"1");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    new POST_Request(this).execute("http://192.168.1.5:3000/api/CheckPassword", shopkeeperJSON.toString());
}

这是发送 JSON 对象到我的本地服务器的 post 方法的 Android 代码:

@Override
protected String doInBackground(String... params) {

    try {
        URL url = new URL(params[0]);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod(REQUEST_METHOD);
        httpCon.setRequestProperty("Accept","application/json");
        //httpCon.setRequestProperty("Content-Type", "application/json");
        httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        //Input
        OutputStream os = httpCon.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        osw.write(params[1]);
        //osw.write("{ ownerEmailId : varuncr7raj@gmail.com, ownerPassword: 1 }");
        osw.flush();
        osw.close();
        os.close();  //don't forget to close the OutputStream
        httpCon.connect();

        //read the inputstream and print it
        String result;
        BufferedInputStream bis = new BufferedInputStream(httpCon.getInputStream());
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int result2 = bis.read();
        while(result2 != -1) {
            buf.write((byte) result2);
            result2 = bis.read();
        }
        result = buf.toString();
        //System.out.println(result);
        Log.e("result", result);
        httpCon.disconnect();
        return result;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

这是我的节点 js 服务器的代码。这只是记录请求的 res.body。

exports.checkPassword = function(req,res){
var data = JSON.stringify(req.body);
//console.log(JSON.stringify(req.body));
var obj = JSON.parse(data);
console.log(obj);
res.send(req.body);
};

代码的输出:-

{ '{"ownerEmailId":"varuncr7raj@gmail.com","ownerPassword":"1"}': '' }

如您所见,JSON 对象正在成为 JSON 对象的 JSON 键。谢谢!任何帮助将不胜感激!

期望的输出:

 {"ownerEmailId":"varuncr7raj@gmail.com","ownerPassword":"1"}

【问题讨论】:

  • 您还没有真正提出明确的问题。您刚刚发布了代码(这是一个好的开始)。结果我真的不知道你需要什么。
  • 对不起!问题已更新为所需的输出!谢谢!!

标签: android arrays json node.js


【解决方案1】:

您说您将以x-www-form-urlencoded 发送参数,但您发送的是JSON。 改成httpCon.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

在服务器端尝试使用你想要的 req.body 属性:

exports.checkPassword = function(req, res) {
    const { ownerEmailId, ownerPassword } = req.body;
    console.log(ownerEmailId, ownerPassword);
    res.send(req.body);
};

【讨论】:

  • 感谢您的回答,但现在的输出是 - undefined undefined
  • req.body 是什么?
  • 我安慰它是空的只是 {}。
  • 您是否已将 BodyParser 安装到您的 Node 服务器上?这应该可以解决您的问题stackoverflow.com/questions/10005939/…
  • 是的,我在我的节点 js 服务器中使用 body-parser!但它确实解决了我的问题!谢谢你,兄弟!你太棒了!
猜你喜欢
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
  • 2015-03-13
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多