Android:怎么解析网上已知格式的json数据文件,并显示到界面上

fengyuzls 2014-11-12 02:24:48
加精
网站上json文件格式
{
"success": true,
"respinfo": null,
"total": 15,
"rows": [
{
"paramVersion": "1",
"menuCode": "B001",
"upMenuCode": "1",
"menuType": "9",
"menuName": "银行",
"menuDisp": null,
"menuLogo": "/images/bank.png",
"menuSort": "100",
"menuPath": null,
"keyWord": "人民广场",
"range": "100",
"lastUptTime": "20141031153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B002",
"upMenuCode": "1",
"menuType": "2",
"menuName": "酒吧",
"menuDisp": null,
"menuLogo": "/images/bar.png",
"menuSort": "200",
"menuPath": null,
"keyWord": "漕河泾",
"range": "200",
"lastUptTime": "20141103153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B003",
"upMenuCode": "1",
"menuType": "9",
"menuName": "公交",
"menuDisp": null,
"menuLogo": "/images/bus.png",
"menuSort": "300",
"menuPath": null,
"keyWord": "豫园",
"range": "400",
"lastUptTime": "20141103153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B004",
"upMenuCode": "1",
"menuType": "1",
"menuName": "电影院",
"menuDisp": null,
"menuLogo": "/images/cinema.png",
"menuSort": "400",
"menuPath": null,
"keyWord": "植物园",
"range": "600",
"lastUptTime": "20141103153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B005",
"upMenuCode": "1",
"menuType": "1",
"menuName": "咖啡厅",
"menuDisp": null,
"menuLogo": "/images/coffee.png",
"menuSort": "500",
"menuPath": null,
"keyWord": "大宁商业广场",
"range": "500",
"lastUptTime": "20141103153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B006",
"upMenuCode": "1",
"menuType": "1",
"menuName": "其他",
"menuDisp": null,
"menuLogo": "/images/else.png",
"menuSort": "600",
"menuPath": null,
"keyWord": "佘山公园",
"range": "800",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B007",
"upMenuCode": "1",
"menuType": "1",
"menuName": "美食",
"menuDisp": null,
"menuLogo": "/images/food.png",
"menuSort": "700",
"menuPath": null,
"keyWord": "灵石公园",
"range": "300",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B008",
"upMenuCode": "B001",
"menuType": "1",
"menuName": "医院",
"menuDisp": null,
"menuLogo": "/images/hospital.png",
"menuSort": "101",
"menuPath": null,
"keyWord": "定位菊花",
"range": "200",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B009",
"upMenuCode": "B001",
"menuType": "1",
"menuName": "酒店",
"menuDisp": null,
"menuLogo": "/images/hotel.png",
"menuSort": "102",
"menuPath": null,
"keyWord": "定位玫瑰",
"range": "200",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B010",
"upMenuCode": "B002",
"menuType": "1",
"menuName": "停车场",
"menuDisp": null,
"menuLogo": "/images/p.png",
"menuSort": "201",
"menuPath": null,
"keyWord": "西餐地址列表",
"range": "700",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B011",
"upMenuCode": "1",
"menuType": "1",
"menuName": "超市",
"menuDisp": null,
"menuLogo": "/images/shopping.png",
"menuSort": "1000",
"menuPath": null,
"keyWord": null,
"range": "800",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "B112",
"upMenuCode": "B003",
"menuType": "1",
"menuName": "公共厕所",
"menuDisp": null,
"menuLogo": "/images/toilet.png",
"menuSort": "1200",
"menuPath": null,
"keyWord": null,
"range": "100",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "G001",
"upMenuCode": "1",
"menuType": "1",
"menuName": "加油站",
"menuDisp": null,
"menuLogo": "/images/gas_station.png",
"menuSort": "800",
"menuPath": null,
"keyWord": "上海动物园",
"range": "1000",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "G002",
"upMenuCode": "1",
"menuType": "1",
"menuName": "公共机构",
"menuDisp": null,
"menuLogo": "/images/gong gong ji gou.png",
"menuSort": "900",
"menuPath": null,
"keyWord": "广中西路肯德基",
"range": "100",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
},
{
"paramVersion": "1",
"menuCode": "G003",
"upMenuCode": "1",
"menuType": "1",
"menuName": "便利店",
"menuDisp": null,
"menuLogo": "/images/store.png",
"menuSort": "1100",
"menuPath": null,
"keyWord": null,
"range": "200",
"lastUptTime": "20141104153210",
"lastUptAcc": null,
"lastUptOrg": null
}
]
}

更具menuSor的大小从小到大排序显示到如下布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:padding="4dp"
android:scaleType="fitXY" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerInParent="true"
android:gravity="center"
android:lines="1"
android:textColor="#ffffff" />

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="85dp"
android:gravity="center"
android:text="周围有啥"
android:textColor="#ffffff"
android:textSize="30dp" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<GridView
android:id="@+id/MyGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:scrollbars="none"
android:verticalSpacing="3dp" />
</LinearLayout>

</LinearLayout>
求大神能指教 谢谢
...全文
3253 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
小新_ 2014-12-19
  • 打赏
  • 举报
回复
楼主,可以用 1.自带的 java解析包 2.gson 3.fastjson
fengyuzls 2014-11-20
  • 打赏
  • 举报
回复
public class XXImpl implementsXX {

	@Override
	public ArrayList<Person> httpGetMainMenuList() throws Exception {
	    Log.i("select===", "httpGetMainMenuList");
	    String str1 = HttpJsonClient.httpGet(http_maingrideurl);
	    Log.i("select===", str1);
	    ArrayList<Person> localArrayList = new ArrayList<Person>();
	    JSONArray localJSONArray = getJSONObject(str1).optJSONArray("arg0");
	    for (int i = 0; ; i++)
	    {
	      if (i >= localJSONArray.length())
	        return localArrayList;
	      JSONObject localJSONObject = localJSONArray.getJSONObject(i);
	      String a= localJSONObject.optString("lastUptOrg");
	      String b= localJSONObject.optString("menuCode");
	      String v= localJSONObject.optString("upMenuCode");
	      String b= localJSONObject.optString("menuType");
	      String n= localJSONObject.optString("menuName");
	      String m= localJSONObject.optString("menuDisp");
	      String r= localJSONObject.optString("menuLogo");
	    ...........................................................
	      
	      Person c= new Person();
	      mm.setA(a);
	      ..............................
....................................
......................................
.........................
	 
	      localArrayList.add(mPerson );
	    }
	}
	
	public Map<String, String> getParams()
	{
	    HashMap<String,String> localHashMap = new HashMap<String,String>();
	    return localHashMap;
	}

	public JSONObject getJSONObject(String paramString)
			    throws Exception
	{
		return (JSONObject)new JSONTokener(paramString).nextValue();
	}


}
这个更方便
fengyuzls 2014-11-20
  • 打赏
  • 举报
回复
满足了 解析已经差不多完成了 再弄 网络下载和 接口调用了
qq343913906 2014-11-20
  • 打赏
  • 举报
回复
GSON 解析方便
王文丰 2014-11-19
  • 打赏
  • 举报
回复
jsonarray足够满足要求
nettman 2014-11-19
  • 打赏
  • 举报
回复
感谢分享,收藏下
qq_18145421 2014-11-18
  • 打赏
  • 举报
回复
不如说说是怎么实现的吧
GW786228836 2014-11-18
  • 打赏
  • 举报
回复
kevin-go 2014-11-18
  • 打赏
  • 举报
回复
可以用JSONObejct解析成JSONObejct对象,再采用gson包下的GSON解析成实体对象, 我的android项目里面就是这么做的, 非常方便
巴里轰 2014-11-17
  • 打赏
  • 举报
回复
用自带的JSONArray 或者用GSON 用GSON需要导个jar包到项目里去 这个jar包自己搜下下载
CodeC 2014-11-17
  • 打赏
  • 举报
回复
一天比一天美好!
fengyuzls 2014-11-17
  • 打赏
  • 举报
回复
网络已经写好 ,实体已定义,已经解析了怎么适配到适配器上 public class HttpUtils { /** * 从指定的URL中获取数组 * * @param urlPath * @return * @throws Exception */ public static String readParse(String urlPath) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream inStream = conn.getInputStream(); while ((len = inStream.read(data)) != -1) { outStream.write(data, 0, len); } inStream.close(); return new String(outStream.toByteArray());// 通过out.Stream.toByteArray获取到写的数据 } /** * 访问数据库并返回JSON数据字符串 * * @param params * 向服务器端传的参数 * @param url * @return * @throws Exception */ public static String doPost(List<NameValuePair> params, String url) throws Exception { String result = null; // 获取HttpClient对象 HttpClient httpClient = new DefaultHttpClient(); // 新建HttpPost对象 HttpPost httpPost = new HttpPost(url); if (params != null) { // 设置字符集 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); // 设置参数实体 httpPost.setEntity(entity); } HttpResponse httpResp = httpClient.execute(httpPost); // 判断是够请求成功 if (httpResp.getStatusLine().getStatusCode() == 200) { // 获取返回的数据 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); } else { Log.i("HttpPost", "HttpPost方式请求失败"); } return result; } /** * 解析 * * @throws JSONException */ public static List<JsonBean> getListBean(String key,String jsonStr){ List<JsonBean> list =new ArrayList<JsonBean>(); try { JSONObject jsonObject =new JSONObject(jsonStr); JSONArray jsonArray =jsonObject.getJSONArray(key); for(int i=0;i<jsonArray.length();i++){ JSONObject jsonObject2 =jsonArray.getJSONObject(i); Map< String, Object> map =new HashMap<String, Object>(); Iterator<String> iterator= jsonObject2.keys(); while(iterator.hasNext()){ String json_key=iterator.next(); String json_value= jsonObject2.getString(json_key); if(json_value == null){ json_value=""; } map.put(json_key, json_value); } list.add((JsonBean) map); } } catch (JSONException e) { e.printStackTrace(); } return list; } package com.zhouweiyousha.json; /** * Json实体类 * * @author admin * */ public class JsonBean { /** * json数据的实体类 * */ private String keyWord; private String menuLogo; private int menuSort; public JsonBean(String keyword, String menuLoge, int menuSort) { super(); this.keyWord = keyword; this.menuLogo = menuLoge; this.menuSort = menuSort; } public JsonBean() { // TODO Auto-generated constructor stub } // 字符输出 public String toString() { return "JsonBean [keyWord=" + keyWord + ", menuLogo=" + menuLogo + ", menuSort=" + menuSort + "]"; } public String getKeyWord() { return keyWord; } public void setKeyWord(String keyWord) { this.keyWord = keyWord; } public String getMenuLogo() { return menuLogo; } public void setMenuLogo(String menuLogo) { this.menuLogo = menuLogo; } public int getMenuSort() { return menuSort; } public void setMenuSort(int menuSort) { this.menuSort = menuSort; } }
lixn_90 2014-11-17
  • 打赏
  • 举报
回复
引用 23 楼 fengyuzls 的回复:
你说的步奏我已经完成啦!接下来要显示主到界面上没有按钮可以按,是个自定义gridview


哦,原来是操作UI啊,就是说加载图片,加载文字,到页面。用gridview形式表现出来呗?快下班了,不给你写代码了。
估计文字显示的问题,你应该会做,就是图片不会处理吧。至于UI更新问题也应该没问题(例如:Handler来更新),那么就是图片的处理:
我看见你有属性menuLogo是图片路径吧(图片在服务器的路径,对吧)那么你就用这个来获取图片吧。
下载此包:
导入名称:import net.tsz.afinal.FinalBitmap;
实例化:FinalBitmap fb = FinalBitmap.create(MainActivity.this);参数就是个context
用法:
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
String imgUrl = "http://192.168.1.1:8080/Test/images/store.png";
fb.display(imageView, imgUrl);

fengyuzls 2014-11-17
  • 打赏
  • 举报
回复
你说的步奏我已经完成啦!接下来要显示主到界面上没有按钮可以按,是个自定义gridview
lixn_90 2014-11-17
  • 打赏
  • 举报
回复
引用 21 楼 fengyuzls 的回复:
18L谢谢但是你还没明白我的意思
你的意思难道不是在手机端接收JSON格式数据,然后解析,解析后,显示在界面么?
fengyuzls 2014-11-17
  • 打赏
  • 举报
回复
18L谢谢但是你还没明白我的意思
lixn_90 2014-11-17
  • 打赏
  • 举报
回复
算了,把这个也写上吧,加上个解析JSONArray数组的。
	/**
	 * 测试
	 * 
	 * @param jsonString
	 * @return
	 */
	public static void getData(String jsonString) {
		try {
			System.out.println("----->>> jsonString = " + jsonString);
			// 获取所有数据
			JSONObject jsonObject = new JSONObject(jsonString);
			System.out.println("----->>> jsonString = " + jsonObject.toString());
			boolean success = jsonObject.getBoolean("success");
			String respinfo = jsonObject.getString("respinfo");
			int total = jsonObject.getInt("total");
			
			//解析JSONArray数组
			JSONArray rows = jsonObject.getJSONArray("rows");
			for (int i = 0; i < rows.length(); i++) {
				JSONObject temp = (JSONObject) rows.get(i);
				String paramVersion = temp.getString("paramVersion");
				String menuCode = temp.getString("menuCode");
				String upMenuCode = temp.getString("upMenuCode");
				String menuType = temp.getString("menuType");
				String menuName = temp.getString("menuName");
				String menuDisp = temp.getString("menuDisp");
				String menuLogo = temp.getString("menuLogo");
				String menuSort = temp.getString("menuSort");
				String menuPath = temp.getString("menuPath");
				String keyWord = temp.getString("keyWord");
				String range = temp.getString("range");
				String lastUptTime = temp.getString("lastUptTime");
				String lastUptAcc = temp.getString("lastUptAcc");
				String lastUptOrg = temp.getString("lastUptOrg");
				
			}
			
			System.out.println("----->>> success = " + success);
			System.out.println("----->>> respinfo = " + respinfo);
			System.out.println("----->>> total = " + total);
			System.out.println("----->>> rows = " + rows.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
lixn_90 2014-11-17
  • 打赏
  • 举报
回复
public class JSONSActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.json_test);
	}
	
	public void onClick(View v){
		String jsonString = "{'success':true,'respinfo':null,'total':15,'rows':[{'test':'1'},{'test':'2'},{'test':'3'}]}";
		switch (v.getId()) {
		case R.id.button1:
			//测试数据
			JsonTools.getData(jsonString);
			break;
		default:
			break;
		}
	}
}
lixn_90 2014-11-17
  • 打赏
  • 举报
回复

        /**
	 * 测试
	 * 
	 * @param jsonString
	 * @return
	 */
	public static void getData(String jsonString) {
		try {
			System.out.println("----->>> jsonString = " + jsonString);
			// 获取所有数据
			JSONObject jsonObject = new JSONObject(jsonString);
			System.out.println("----->>> jsonString = " + jsonObject.toString());
			boolean success = jsonObject.getBoolean("success");
			String respinfo = jsonObject.getString("respinfo");
			int total = jsonObject.getInt("total");
			JSONArray rows = jsonObject.getJSONArray("rows");
			
			System.out.println("----->>> success = " + success);
			System.out.println("----->>> respinfo = " + respinfo);
			System.out.println("----->>> total = " + total);
			System.out.println("----->>> rows = " + rows.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
fengyuzls 2014-11-17
  • 打赏
  • 举报
回复
怎么显示到适配器呢?不能使用gson的求指教,我list.add((JsonBean) map);已经添加到JSONBean里面了 怎么把他显示出来 额 ,还要怎么把 文字和图片显示到 public class JsonBeanAdapter extends BaseAdapter { private Context mContext; private LayoutInflater mInflater; private List<JsonBean> data; private AsyncImageLoader imageLoader; // 实现内部类的方法 public JsonBeanAdapter(Context context, List<JsonBean> data) { super(); this.mContext = context; this.data = data; imageLoader =new AsyncImageLoader(); mInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { return data.size(); } public Object getItem(int position) { return data.get(position); } public long getItemId(int position) { return position; } class ViewHolder { private TextView title; private ImageView imageView; } // getView方法的实现 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; JsonBean jsonBean=data.get(position); if(convertView ==null){ holder =new ViewHolder(); convertView=mInflater.inflate(R.layout.griditem, null); holder.imageView=(ImageView)convertView.findViewById(R.id.imageView); holder.title=(TextView)convertView.findViewById(R.id.title); convertView.setTag(holder);; }else{ holder=(ViewHolder)convertView.getTag(); } return convertView; } }这个自定义的适配器中
加载更多回复(13)

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧