Java 截击:如何通过HashMap解析带有整数的数据字符串
如何通过HashMap解析带有整数的数据字符串 我有json的字符串和整数数据 HashMap中的问题是,他只接受字符串或整数值 这是Json的结构Java 截击:如何通过HashMap解析带有整数的数据字符串,java,android,json,android-volley,Java,Android,Json,Android Volley,如何通过HashMap解析带有整数的数据字符串 我有json的字符串和整数数据 HashMap中的问题是,他只接受字符串或整数值 这是Json的结构 { "status":true, "message":"Get Products Successfully", "products":[ { "p_id":48, "p_name":"Ford Red", "p_descrption":"rrrrrrrrrrr
{
"status":true,
"message":"Get Products Successfully",
"products":[
{
"p_id":48,
"p_name":"Ford Red",
"p_descrption":"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr",
"p_price":15000,
"p_offer_price":12000,
"p_image":"myLinkimage"
}
]
}
这是我的java代码
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "myLink", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("p_name","mac");
params.put("p_descrption","mac mac 2018");
params.put("p_price","10000");
params.put("p_offer_price","8000");
params.put("p_image","https://d.top4top.net/p_105212t9r1.jpg");
return params;
}
};
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
在我的json结构中,它是一个整数,
如何像解析整数值一样解析这些数据?您不需要解析任何东西,而是按照预期使用JsonObjectRequest 这是从
getParams
方法中移动映射(并可能完全删除该方法),然后在截击调用之前创建有效负载
JSONObject payload = new JSONObject();
JSONArray products = new JSONArray();
JSONObject product = new JSONObject();
product.putInt("price", 100);
products.add(product);
payload.putArray("products", products);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "myLink", payload,
new Response.Listener<JSONObject>() {
JSONObject有效负载=新的JSONObject();
JSONArray products=新的JSONArray();
JSONObject产品=新的JSONObject();
产品价格(“价格”,100);
产品。添加(产品);
有效载荷。putArray(“产品”,产品);
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,“myLink”,有效负载,
新的Response.Listener(){
例如,如果您想解析响应,可以直接调用response.getInt
如果您愿意,您可以将Volley与Gson结合起来,正如Android文档中所指出的那样,这使得这个实现更加简洁,或者对w/Gson进行改造也可以类似地工作
Integer.valueOf
或Integer.parseInt
也许?搜索“Java字符串到整数”没有返回任何结果?我真的看不出您的问题。可能只是解释得不清楚。您在分析文章顶部的JSON结构时遇到问题吗?或者您在getParams()中的参数方面遇到问题吗
?还是像AKSW指出的那样,您的问题只是简单的类型转换?@Barns是的,参数中的问题,我需要设置hashMap字符串和integer中的数据。这应该不是问题。只需将整数
值转换为字符串
,并将它们添加到getParams()中的hashMap
)
。您正在将值作为参数发送到POST
请求。无论如何,这些参数将作为字符串
发送。如果需要,您可以在Web服务器上进行类型转换。您不应该在此处使用getParams
…POST后的null
参数应该是要发送到t的JSONObjecthe服务器
JSONObject payload = new JSONObject();
JSONArray products = new JSONArray();
JSONObject product = new JSONObject();
product.putInt("price", 100);
products.add(product);
payload.putArray("products", products);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "myLink", payload,
new Response.Listener<JSONObject>() {