Gson应用《Json解析》


/**

 *
 * 转载请标明出处:http://blog.csdn.net/u013598111/article/details/51277085

 *   @author:【JunTao_sun】
 *
 *
*/

package com.example.gson;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.JsonReader;
import android.util.Log;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView text;
	private Gson gson;
	private String TAG;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (TextView) findViewById(R.id.text);
		gson = new Gson();
		Button btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				jsonRed();
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {

							getDatas();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}).start();

			}
		});

	}

	private void getDatas() throws IOException {
		URL url = new URL("http://192.168.0.102:8080/myweb/json");
		final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setDoInput(true);
		conn.setDoInput(true);
		conn.connect();
		InputStream ins = null;

		if (conn.getResponseCode() == 200) {

			ins = conn.getInputStream();

		}
		if (ins != null) {

			final String result = parseInputStream(ins, conn);

			// 第一种 解析jsons.jsp格式的 下面会贴服务器 jsp代码
			// final Vegetable v= gson.fromJson(result, Vegetable.class);
			// 第二种 解析jsons2.jsp格式的
			List<Menu> list = new ArrayList<Menu>();
			final List<Menu> menu = gson.fromJson(result,
					new TypeToken<List<Menu>>() {
					}.getType());
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Log.e("jt", "" + result);
					

<span style="white-space:pre">					</span>// 第一种 测试text.setText( v.getResult().getData().get(0).getImtro());
<span style="white-space:pre">					</span>// 第二种测试text.setText( menu.get(0).getImtro());

				}
			});
		}

	}

	/**
	 * 
	 * 解析获取网络的输入流
	 *  
	 **/
	private String parseInputStream(InputStream ins, HttpURLConnection conn)
			throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int len = 0;
		byte[] buffs = new byte[1024];
		while ((len = ins.read(buffs)) != -1) {
			out.write(buffs, 0, len);

		}

		String srt = new String(out.toByteArray());
		out.close();
		ins.close();
		conn.disconnect();
		return srt;
	}

	public void jsonRed() {
		StringBuilder str = new StringBuilder();
		
		Map<String, String> map = new HashMap<String, String>();
        map.put("adress", "广东");
		map.put("name", "橘子");
		map.put("age", "18");
		map.put("like", "bread");
       //构建一个json对象
		String mapj = gson.toJson(map);
		str.append(mapj);
		text.setText(str);
		str.append("\n");
		List<String> list = new ArrayList<String>();
		list.add("蛋糕");
		list.add("水果");
		list.add("面包");
		//构建一个数组对象
		String listj = gson.toJson(list);
		str.append(listj);
		text.setText(str);
		str.append("\n");
		//JsonReader的使用
		//解析单个对象
		StringReader sw = new StringReader(mapj);
		JsonReader reader = new JsonReader(sw);
		try {
			reader.beginObject();

			while (reader.hasNext()) {
				String key = reader.nextName();
				if ("adress".equals(key)) {
					System.out.println(reader.nextString());
				}
				if ("name".equals(key)) {
					System.out.println(reader.nextString());
				}
				if ("age".equals(key)) {
					System.out.println(reader.nextString());
				}
				if ("like".equals(key)) {
					System.out.println(reader.nextString());
				}

			}
			reader.endObject();
			// 解析数组对象
			Map<String, String> map2 = new HashMap<String, String>();
			map2.put("adress", "福建");
			map2.put("name", "苹果");
			map2.put("age", "20");
			map2.put("like", "cake");
			
			Map<String, String> map3 = new HashMap<String, String>();

			map3.put("adress", "背景");
			map3.put("name", "香蕉");
			map3.put("age", "19");
			map3.put("like", "LOL");
			List<Map> arrObject = new ArrayList<Map>();
            arrObject.add(map2);
			arrObject.add(map3);
			
			//把json格式字符串 写入文件
			File file=writerJsonToFile(arrObject);
			//读取文件里的json数据
			readJsonFromFile(file);
			
			String arrO = gson.toJson(arrObject);
			str.append(arrO);
			text.setText(str);
			str.append("\n");
			JsonReader reader1 = new JsonReader(new StringReader(arrO));
			reader1.beginArray();
			while (reader1.hasNext()) {
				reader1.beginObject();
				while (reader1.hasNext()) {
					String key = reader1.nextName();
					if ("adress".equals(key)) {
						System.out.println(reader1.nextString());
					}
					if ("name".equals(key)) {
						System.out.println(reader1.nextString());
					}
					if ("age".equals(key)) {
						System.out.println(reader1.nextString());
					}
					if ("like".equals(key)) {
						System.out.println(reader1.nextString());
					}

				}
				reader1.endObject();
			}
			reader1.endArray();
			// 第三种 解析对象包含对象
			String json3 = "{ \"name\":\"juntao\",\"age\":25,\"address\":{\"country\":\"中国\",\"province\":\"广东\",\"city\":\"潮州\"}}";
			JsonReader reader2 = new JsonReader(new StringReader(json3));
			reader2.beginObject();
			while (reader2.hasNext()) {
				String key = reader2.nextName();

				if ("name".equals(key)) {
					System.out.println(reader2.nextString());
				}
				if ("age".equals(key)) {
					System.out.println(reader2.nextInt());
				}
				if ("address".equals(key)) {
					reader2.beginObject();
					while (reader2.hasNext()) {
						String theKey =reader2.nextName();
						if ("country".equals(theKey)) {
							System.out.println(reader2.nextString());
						}
						if ("province".equals(theKey)) {
							System.out.println(reader2.nextString());
						}
						if ("city".equals(theKey)) {
							System.out.println(reader2.nextString());
						}
					}
					reader2.endObject();
				}

			}
			reader2.endObject();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// gson.fromJson(new InputStreamReader(new FileInputStream("")), new
		// TypeToken<List<Map<String, String>>>() {}.getType());

	}
	private  File writerJsonToFile(List<Map> mapList) {
		
	
		
		
		File file = new File(getFilesDir().getAbsolutePath(),"gson");
		if(!file.exists()){
			try {
				file.createNewFile();
				Log.e(TAG, "getAbsolutePath"+file.getAbsolutePath());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		OutputStream out;
		try {
			out = new FileOutputStream(file);
			JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));//设计编码
			gson.toJson(mapList, new TypeToken<List<Map<String, String>>>() {}.getType(), writer);
			writer.flush();
			writer.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return file;
	}
	private  void readJsonFromFile( File file) {
		InputStream input;
		try {
			input = new FileInputStream(file);
			JsonReader reader = new JsonReader(new InputStreamReader(input));
			
//			List<Map<String,String>> content = gson.fromJson(reader,new TypeToken<List<Map<String, String>>>() {}.getType());
			List<Map<String, String>> content = gson.fromJson(new InputStreamReader(input),new TypeToken<List<Map<String, String>>>() {}.getType());

			for(int i=0;i<content.size();i++){
				Map<String, String> map=content.get(i);
			
			for(Map.Entry<String, String> e:map.entrySet()){
				System.out.print("key"+e.getKey()+"value"+e.getValue()+"\n");
				reader.close();
		} 
			}} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}


}


下面是三种集合的 json格式
JsonReader 解析json 打印出来的结果:


最后是请求服务器 返回的两种 json 样式解析:


第一种  复杂的 json样式:

<span style="font-size:18px;"><%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>
{
    "resultcode": "200",
    "reason": "Success",
    "result": {
        "data": [{
            "id": "45",
            "title": "秘制红烧肉",
            "tags": "家常菜;热菜;烧;煎;炖;红烧;炒锅",
            "imtro": "做红烧肉的豆亲们很多,大家对红烧肉的热爱更不用我说,从名字上就能反映出来。一些高手们对红烧肉的认识更是令我佩服,单单就红烧肉的做法、菜谱都看得我是眼花缭乱,口水横流。单纯的红烧肉我平时还真没做过,再不抓紧时间做一回解解馋,不是对不起别人,而是太对不起我自己了! 这道菜的菜名用了秘制二字来形容,当然是自己根据自己多年吃货的经验想象出来的,我不介意把自己的做法与大家共享,只为大家能同我一样,吃到不同口味的红烧肉。不同的人们根据自己的习惯都有不同的做法,味道也不尽相同。我的秘制的关键就是必须用玫瑰腐乳、冰糖和米醋这三种食材,腐乳和冰糖可以使烧出来的肉色泽红亮,米醋能解腻,令肥肉肥而不腻,此法烧制的红烧肉软糯中略带咸甜,的确回味无穷!",
            "ingredients": "五花肉,500g",
            "burden": "玫瑰腐乳,适量;盐,适量;八角,适量;草果,适量;香叶,适量;料酒,适量;米醋,适量;生姜,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/0\/45_854851.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_0824e37faf00b71e.jpg",
                "step": "1.将五花肉煮至断生状态"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_b6d7329b703f6e85.jpg",
                "step": "2.切成大小一致的块"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_6ee9e8dab0516333.jpg",
                "step": "3.放在锅内煎"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_b9afd6d4dd81f55c.jpg",
                "step": "4.入生姜"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_d0170fbe236421f9.jpg",
                "step": "5.放八角草果各一个,香叶一片"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_639b12210745fa41.jpg",
                "step": "6.放冰糖"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_c25e0cedd2012f45.jpg",
                "step": "7.加料酒"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_eb68327980f022dd.jpg",
                "step": "8.加玫瑰腐乳和腐乳汁及适量盐"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_ac17263a11507a41.jpg",
                "step": "9.加米醋"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_f5489af5d12b4930.jpg",
                "step": "10.加水继续炖"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_8e0cf83cb7306281.jpg",
                "step": "11.直至肉变软糯汤汁收干即可"
            }]
        },
        {
            "id": "52",
            "title": "经典红烧肉",
            "tags": "家常菜;咸;半小时-1小时;孕妇;青少年;老人;白领;晚餐;红烧;营养;增强抵抗力;全菜系;1-2人;待客菜;锅子",
            "imtro": "红烧肉是热菜菜谱之一,以五花肉为制作主料,红烧肉的做法各地也会稍有不同。南方习惯用酱油(老抽)调色,而北方则偏爱炒糖色儿,红烧肉也是我拿手菜之一,不管是逢年过节还是亲朋聚会都不会少了诱人的红烧肉。红烧肉的特点;浓油赤酱,肥而不腻,入口酥软即化。",
            "ingredients": "五花肉,1000g",
            "burden": "葱,适量;姜,适量;蒜,适量;八角,2粒;桂皮,1块;干辣椒,1个;酱油,适量;冰糖,适量;盐,适量;料酒,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/1\/52_759155.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_b02a6aa276e3352f.jpg",
                "step": "1.五花肉洗净切成大块。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_0cfde36e8061dd9c.jpg",
                "step": "2.冷水把肉下锅烧开,把肉焯水。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_4bbe9a903c8b144e.jpg",
                "step": "3.准备好 八角 桂皮 葱姜蒜 辣椒。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_887688f6d7716106.jpg",
                "step": "4.把锅烧热,下入焯水的五花肉,小火煸炒至五花肉出油。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_72ddd29b5fba09c3.jpg",
                "step": "5.下入葱姜蒜 八角 桂皮 辣椒继续煸炒至肉色微黄。盛出。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_90765e3bf8a33c53.jpg",
                "step": "6.锅中留底油放入冰糖小火熬至起沫成棕红色。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_95ddaa107b65158b.jpg",
                "step": "7.倒入煸炒的五花肉翻炒均匀,使每块肉均匀沾满糖色,"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_68a1aa355f2c62e9.jpg",
                "step": "8.加入料酒、酱油"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_b4391bab5b4c70b7.jpg",
                "step": "9.加入开水没过肉,盖上锅盖开火煮沸,调小火炖40分钟,"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_22e562a234ed1891.jpg",
                "step": "10.烧至肉熟烂、把汤汁收浓即可。"
            }]
        },
        {
            "id": "92",
            "title": "红烧肉",
            "tags": "家常菜;快手菜",
            "imtro": "红烧肉,各地各家的做法稍有不同味道即大不一样,中餐的精妙在于食材随意性而产生的变化,南方习惯用酱油(老抽)调色,而北方则偏爱炒糖色。 女人不要为了保持好身材,刻意与美味的肉类食品绝缘哦。其实,很多科学证明,适当的吃肉并不会增加额外的脂肪。 猪肉经过小火煸炒出油后,炖出来的红炒肉是肥而不腻,软烂入味,非常的好吃美味,也非常适合咱们的健康理念,即少油,又解馋,又解腻,下面,为你推荐这种红烧肉的经典做法,不用一滴油就可以做出美味健康的红烧肉。",
            "ingredients": "猪后臀尖,1000g",
            "burden": "葱段,适量;八角,2个;干辣椒,4个;香叶,4片;桂皮,1块;鲜姜,1块;干山楂片,4片;黄油,适量;老抽,适量;生抽,适量;白糖,适量;开水,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/1\/92_512827.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_472370e29b980b31.jpg",
                "step": "1.猪肉清洗干净切成方块,冷水下锅,水开后撇去上面的浮沫。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_04f90c14513aa139.jpg",
                "step": "2.焯好的肉块捞出,用温水清洗干净。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_50af1fae3911794d.jpg",
                "step": "3.准备好调料:干辣椒,桂皮,香叶,八角,(大蒜也可以不放)。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_12f839ee0920c03b.jpg",
                "step": "4.做锅开小火,不放油,把肉块放入反复煸炒,煸炒至肉块有点焦香,有油渗出,关火把肉块捞出。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_bfdc35cde7da929b.jpg",
                "step": "5.做锅,用肉块煸出的猪油烧热,放入八角,干辣椒,桂皮,香叶煸炒出香味。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_e21dddc1ffcad36b.jpg",
                "step": "6.放入肉块煸炒1-2分钟后捞出备用。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_89ba401801a762fe.jpg",
                "step": "7.用锅中剩下的余油放入2勺白糖(这个量可以根据自己的喜好添加,喜欢吃甜一点的就多放点糖),开小火煸炒白糖。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_33f84ea85dcd11c9.jpg",
                "step": "8.这是糖色变化的过程"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_bbf69240b5f7211d.jpg",
                "step": "9.当糖起小泡,颜色变红就可以了。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_114bbdd5ef3d1255.jpg",
                "step": "10.这时快速烹上1勺热水,加入肉块煸炒至肉块颜色变红。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_831b33673ac8c507.jpg",
                "step": "11.煸炒至猪肉块上色后,加入适量生抽,老抽(不可多放,放多了颜色就发黑了),黄酒煸炒。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_a377c03180cb6b26.jpg",
                "step": "12.加入开水(多放点水,最好一次加足,肉炖熟了可以大火收汤,水少了,中途加水就影响肉质的口感了),再把姜块,葱段,山楂片放入。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_127b81298ad6cf93.jpg",
                "step": "13.盖上锅盖,大火烧开,小火炖煮1个小时。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_59ebcf1a60e54619.jpg",
                "step": "14.炖煮了一个小时了,汤汁也差不多剩一半了,这时加入6克盐(盐可以根据自己的口味添加,在还有汤的情况下,不要尝出咸味正好,等汤汁收浓就会咸了),小火继续炖煮。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_f4cbb0c04220428e.jpg",
                "step": "15.当品尝到肉质软烂,就可以大火收汁,一锅肥而不腻,肉质酥烂,诱人食欲的红烧肉就炖好了,赶快就着小酒,大快朵颐的享受吧"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_372112ff28389c98.jpg",
                "step": "16.炎热的夏季,吃一顿少油,解腻,又美味的红烧肉大餐。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_1630281ff0350105.jpg",
                "step": "17.再来一张,想吃吗,那就赶紧动手吧,呵呵。"
            }]
        }],
        "totalNum": "306",
        "pn": "1",
        "rn": "3"
    },
    "error_code": 0
}
</span>

final Vegetable v= gson.fromJson(result, Vegetable.class); 把json数据反射到Vegetable对象

第二种 简单的json样式:

<span style="font-size:18px;"><%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>
[{
            "id": "45",
            "title": "秘制红烧肉",
            "tags": "家常菜;热菜;烧;煎;炖;红烧;炒锅",
            "imtro": "做红烧肉的豆亲们很多,大家对红烧肉的热爱更不用我说,从名字上就能反映出来。一些高手们对红烧肉的认识更是令我佩服,单单就红烧肉的做法、菜谱都看得我是眼花缭乱,口水横流。单纯的红烧肉我平时还真没做过,再不抓紧时间做一回解解馋,不是对不起别人,而是太对不起我自己了! 这道菜的菜名用了秘制二字来形容,当然是自己根据自己多年吃货的经验想象出来的,我不介意把自己的做法与大家共享,只为大家能同我一样,吃到不同口味的红烧肉。不同的人们根据自己的习惯都有不同的做法,味道也不尽相同。我的秘制的关键就是必须用玫瑰腐乳、冰糖和米醋这三种食材,腐乳和冰糖可以使烧出来的肉色泽红亮,米醋能解腻,令肥肉肥而不腻,此法烧制的红烧肉软糯中略带咸甜,的确回味无穷!",
            "ingredients": "五花肉,500g",
            "burden": "玫瑰腐乳,适量;盐,适量;八角,适量;草果,适量;香叶,适量;料酒,适量;米醋,适量;生姜,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/0\/45_854851.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_0824e37faf00b71e.jpg",
                "step": "1.将五花肉煮至断生状态"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_b6d7329b703f6e85.jpg",
                "step": "2.切成大小一致的块"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_6ee9e8dab0516333.jpg",
                "step": "3.放在锅内煎"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_b9afd6d4dd81f55c.jpg",
                "step": "4.入生姜"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_d0170fbe236421f9.jpg",
                "step": "5.放八角草果各一个,香叶一片"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_639b12210745fa41.jpg",
                "step": "6.放冰糖"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_c25e0cedd2012f45.jpg",
                "step": "7.加料酒"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_eb68327980f022dd.jpg",
                "step": "8.加玫瑰腐乳和腐乳汁及适量盐"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_ac17263a11507a41.jpg",
                "step": "9.加米醋"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_f5489af5d12b4930.jpg",
                "step": "10.加水继续炖"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_8e0cf83cb7306281.jpg",
                "step": "11.直至肉变软糯汤汁收干即可"
            }]
        },
        {
            "id": "52",
            "title": "经典红烧肉",
            "tags": "家常菜;咸;半小时-1小时;孕妇;青少年;老人;白领;晚餐;红烧;营养;增强抵抗力;全菜系;1-2人;待客菜;锅子",
            "imtro": "红烧肉是热菜菜谱之一,以五花肉为制作主料,红烧肉的做法各地也会稍有不同。南方习惯用酱油(老抽)调色,而北方则偏爱炒糖色儿,红烧肉也是我拿手菜之一,不管是逢年过节还是亲朋聚会都不会少了诱人的红烧肉。红烧肉的特点;浓油赤酱,肥而不腻,入口酥软即化。",
            "ingredients": "五花肉,1000g",
            "burden": "葱,适量;姜,适量;蒜,适量;八角,2粒;桂皮,1块;干辣椒,1个;酱油,适量;冰糖,适量;盐,适量;料酒,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/1\/52_759155.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_b02a6aa276e3352f.jpg",
                "step": "1.五花肉洗净切成大块。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_0cfde36e8061dd9c.jpg",
                "step": "2.冷水把肉下锅烧开,把肉焯水。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_4bbe9a903c8b144e.jpg",
                "step": "3.准备好 八角 桂皮 葱姜蒜 辣椒。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_887688f6d7716106.jpg",
                "step": "4.把锅烧热,下入焯水的五花肉,小火煸炒至五花肉出油。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_72ddd29b5fba09c3.jpg",
                "step": "5.下入葱姜蒜 八角 桂皮 辣椒继续煸炒至肉色微黄。盛出。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_90765e3bf8a33c53.jpg",
                "step": "6.锅中留底油放入冰糖小火熬至起沫成棕红色。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_95ddaa107b65158b.jpg",
                "step": "7.倒入煸炒的五花肉翻炒均匀,使每块肉均匀沾满糖色,"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_68a1aa355f2c62e9.jpg",
                "step": "8.加入料酒、酱油"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_b4391bab5b4c70b7.jpg",
                "step": "9.加入开水没过肉,盖上锅盖开火煮沸,调小火炖40分钟,"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_22e562a234ed1891.jpg",
                "step": "10.烧至肉熟烂、把汤汁收浓即可。"
            }]
        },
        {
            "id": "92",
            "title": "红烧肉",
            "tags": "家常菜;快手菜",
            "imtro": "红烧肉,各地各家的做法稍有不同味道即大不一样,中餐的精妙在于食材随意性而产生的变化,南方习惯用酱油(老抽)调色,而北方则偏爱炒糖色。 女人不要为了保持好身材,刻意与美味的肉类食品绝缘哦。其实,很多科学证明,适当的吃肉并不会增加额外的脂肪。 猪肉经过小火煸炒出油后,炖出来的红炒肉是肥而不腻,软烂入味,非常的好吃美味,也非常适合咱们的健康理念,即少油,又解馋,又解腻,下面,为你推荐这种红烧肉的经典做法,不用一滴油就可以做出美味健康的红烧肉。",
            "ingredients": "猪后臀尖,1000g",
            "burden": "葱段,适量;八角,2个;干辣椒,4个;香叶,4片;桂皮,1块;鲜姜,1块;干山楂片,4片;黄油,适量;老抽,适量;生抽,适量;白糖,适量;开水,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/1\/92_512827.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_472370e29b980b31.jpg",
                "step": "1.猪肉清洗干净切成方块,冷水下锅,水开后撇去上面的浮沫。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_04f90c14513aa139.jpg",
                "step": "2.焯好的肉块捞出,用温水清洗干净。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_50af1fae3911794d.jpg",
                "step": "3.准备好调料:干辣椒,桂皮,香叶,八角,(大蒜也可以不放)。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_12f839ee0920c03b.jpg",
                "step": "4.做锅开小火,不放油,把肉块放入反复煸炒,煸炒至肉块有点焦香,有油渗出,关火把肉块捞出。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_bfdc35cde7da929b.jpg",
                "step": "5.做锅,用肉块煸出的猪油烧热,放入八角,干辣椒,桂皮,香叶煸炒出香味。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_e21dddc1ffcad36b.jpg",
                "step": "6.放入肉块煸炒1-2分钟后捞出备用。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_89ba401801a762fe.jpg",
                "step": "7.用锅中剩下的余油放入2勺白糖(这个量可以根据自己的喜好添加,喜欢吃甜一点的就多放点糖),开小火煸炒白糖。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_33f84ea85dcd11c9.jpg",
                "step": "8.这是糖色变化的过程"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_bbf69240b5f7211d.jpg",
                "step": "9.当糖起小泡,颜色变红就可以了。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_114bbdd5ef3d1255.jpg",
                "step": "10.这时快速烹上1勺热水,加入肉块煸炒至肉块颜色变红。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_831b33673ac8c507.jpg",
                "step": "11.煸炒至猪肉块上色后,加入适量生抽,老抽(不可多放,放多了颜色就发黑了),黄酒煸炒。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_a377c03180cb6b26.jpg",
                "step": "12.加入开水(多放点水,最好一次加足,肉炖熟了可以大火收汤,水少了,中途加水就影响肉质的口感了),再把姜块,葱段,山楂片放入。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_127b81298ad6cf93.jpg",
                "step": "13.盖上锅盖,大火烧开,小火炖煮1个小时。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_59ebcf1a60e54619.jpg",
                "step": "14.炖煮了一个小时了,汤汁也差不多剩一半了,这时加入6克盐(盐可以根据自己的口味添加,在还有汤的情况下,不要尝出咸味正好,等汤汁收浓就会咸了),小火继续炖煮。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_f4cbb0c04220428e.jpg",
                "step": "15.当品尝到肉质软烂,就可以大火收汁,一锅肥而不腻,肉质酥烂,诱人食欲的红烧肉就炖好了,赶快就着小酒,大快朵颐的享受吧"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_372112ff28389c98.jpg",
                "step": "16.炎热的夏季,吃一顿少油,解腻,又美味的红烧肉大餐。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_1630281ff0350105.jpg",
                "step": "17.再来一张,想吃吗,那就赶紧动手吧,呵呵。"
            }]
        }]
    
   
</span>

final List<Menu> menu = gson.fromJson(result,new TypeToken<List<Menu>>() {}.getType()); 

反射构建集合类对象 适用json数组对象.

下面是 第一种解析json 用的映射类:


<span style="font-size:18px;">package com.example.gson;

import java.util.List;

public class Vegetable {
	
	public int resultcode;
	public String reason;
	public Result result;
	public String error_code;


	
	
	public int getResultcode() {
		return resultcode;
	}
	public void setResultcode(int resultcode) {
		this.resultcode = resultcode;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	public Result getResult() {
		return result;
	}
	public void setResult(Result result) {
		this.result = result;
	}
	public String getError_code() {
		return error_code;
	}
	public void setError_code(String error_code) {
		this.error_code = error_code;
	}</span>

<span style="font-size:18px;">package com.example.gson;

import java.util.List;

public class Result {

	public List<Menu> data;
	public String pn;
	public  String rn;
   public List<Menu> getData() {
		return data;
	}
	public void setData(List<Menu> data) {
		this.data = data;
	}
	public String getPn() {
		return pn;
	}
	public void setPn(String pn) {
		this.pn = pn;
	}
	public String getRn() {
		return rn;
	}
	public void setRn(String rn) {
		this.rn = rn;
	}
	


}</span>

<span style="font-size:18px;">package com.example.gson;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class Menu {
	  public  String id;
	  public  String title;
	  public String tags;
	  public  String imtro;
	  public  String ingredients;
	  public  String burden;
	 //错误了
//	  public List<Albums> albums;
	  public ArrayList<String> albums;
	  
	  public List<Steps> steps;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTags() {
		return tags;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	public String getImtro() {
		return imtro;
	}
	public void setImtro(String imtro) {
		this.imtro = imtro;
	}
	public String getIngredients() {
		return ingredients;
	}
	public void setIngredients(String ingredients) {
		this.ingredients = ingredients;
	}
	public String getBurden() {
		return burden;
	}
	public void setBurden(String burden) {
		this.burden = burden;
	}
	public ArrayList<String> getAlbums() {
		return albums;
	}
	public void setAlbums(ArrayList<String> albums) {
		this.albums = albums;
	}
	public List<Steps> getSteps() {
		return steps;
	}
	public void setSteps(List<Steps> steps) {
		this.steps = steps;
	}

}
</span>
之前把把 albums 属性 写成集合对象 public List<Albums> albums; 出错。
  public ArrayList<String> albums 改成字符串集合;

<span style="font-size:18px;">package com.example.gson;

public class Steps {

	public  String   img;
	public String step;
	public String getImg() {
		return img;
	}
	public void setImg(String img) {
		this.img = img;
	}
	public String getStep() {
		return step;
	}
	public void setStep(String step) {
		this.step = step;
	}
	
}</span>








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值