如何使用JSONParser类方法制作JSONObject [英] How to make a JSONObject using JSONParser class method

查看:246
本文介绍了如何使用JSONParser类方法制作JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我的标题是否完全适合我的问题,但这是我能想到的最好的结果.

Not sure if my title quite suits my problem, but that's the best I could come up with.

这是我来自网站的JSONString:

This is my JSONString from a website:

{"0":{"productname":"Varios - NEUTROGENA PROTECTOR LABIAL SPF5","imageurl":"http://ecx.images-amazon.com/images/I/41cm5BHMGtL._SL160_.jpg","producturl":"","price":"1.72","currency":"GBP","saleprice":"","storename":"N/A"}}

我想使用提供的类制作JSONObject 此处

I want to make a JSONObject with the class provided here.

这是我到目前为止所做的:

This is what I have done so far:

JSONParser jsonparser = new JSONParser(); 
String jsonString = null;
JSONObject jsonObject = jsonparser.makeHttpRequest(url, GET, params);

makeHttpRequest 方法接受3个参数:

  1. URL-我在上面放置从上面提供JSONResponse的网站的URL;
  2. 字符串(GET或POST);
  3. List<NameValuePair>

我不知道应该在列表中添加什么以使我的方法起作用.我必须以哪种格式将其放入列表中,以使该方法返回合适的JSONObject,这对于以后(轻松)使用将是有益的.

I don't know what should I put in the list to make my method work. In which format do I have to put it in the List to make the method return a propper JSONObject which would be good for later (easy) use.

推荐答案

根据本教程,并根据该教程,使用org.apache.http.message.BasicNameValuePair类将列表用于将参数列表(如有必要)传递给httprequest. NameValuePair本身是一个接口,其文档在

According to the question in the linked post, it is followed from this tutorial, and according to that tutorial, the list is used to pass the list of params if necessary to the httprequest using the org.apache.http.message.BasicNameValuePair class. The NameValuePair itself is an interface and its documentation is given in this link

您可以将值添加到params列表中,如下所示.取自本教程:

You could add values to the params list as shown below. Taken from the tutorial:

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));

因此,您只需要通过更改添加到列表的键值对将所需的参数传递给URL.

So you just have to pass the params that you need to the URL by changing the key-value pairs added to the list.

希望这会有所帮助.

这篇关于如何使用JSONParser类方法制作JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆