JAVA simple parsing json file

Keywords: Java JSON

JAVA simple parsing json file

First, put the json file I want to parse:

{ "resultcode":"200", "reason":"Success", "result":{ "data":[ { "id":"14", "title":"Pork braised in brown sauce", "tags":"Home Dishes;Aged;salty;Semih.-1 hour;Teenagers;white collar;Braise in soy sauce;1-2 people;Wok", "imtro":"With quail eggs, I wish you all a happy new year.", "ingredients":"Streaky pork,250g;radish,100g;Quail egg,20 individual", "burden":"oil,Appropriate amount;salt,Appropriate amount", "albums":[ "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/t\/0\/14_359794.jpg" ], "steps":[ { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_706ca81e0bbecefe.jpg", "step":"1.With skin and streaky pork, quail eggs, white radish spare." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_dc89a7e8f2823202.jpg", "step":"2.The ingredients are ready for use." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_ae5efcd7698c7a51.jpg", "step":"3.Peel quail eggs and set aside." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_6246ae9cde95a443.jpg", "step":"4.Wash the radish and cut into large pieces." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_fa36e18ba88ba812.jpg", "step":"5.Pork, wash and cut into large pieces." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_fb6017cd9ea66698.jpg", "step":"6.Blanch and set aside." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_79b1cc9fb722b4dd.jpg", "step":"7.Heat the oil pan and put in the meat." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_0a45336653b0eb33.jpg", "step":"8.Fry until light yellow." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_cb483431930bb8cd.jpg", "step":"9.Add sugar." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_93eababb3d6f328a.jpg", "step":"10.Stir fry until golden." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_b1672001decab1dc.jpg", "step":"11.Add cooking wine and soy sauce, stir well." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_03ce88cb45776456.jpg", "step":"12.Put in dried red pepper, ginger, star anise, fragrant leaves, pepper, stir fry out the fragrance, then add some water." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_5e067f35b2297a47.jpg", "step":"13.After cooking for more than ten minutes, add radish and quail eggs." }, { "img":"http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_7f8a56f9b8498703.jpg", "step":"14.Cook for more than ten minutes. The meat and radish are soft and rotten. Add some salt to the juice." } ] } ], "totalNum":1, "pn":0, "rn":1 }, "error_code":0 }

The structure is roughly as follows:

 

The json file returned after calling the api is just like this. It's very long and messy. This document is a recipe. Now I only need to use the name, ingredients, methods and other information.

Put the code to parse json first:

JSONObject object = JSONObject.fromObject(result);
            JSONObject ob=object.getJSONObject("result");
            JSONArray ob1=ob.getJSONArray("data");
            JSONObject o2=ob1.getJSONObject(0);
            JSONArray po=o2.getJSONArray("steps");
            
            if(object.getInt("error_code")==0){
                System.out.println(o2.getString("title"));
                System.out.println(o2.getString("ingredients"));
                System.out.println(o2.getString("burden"));
                 for(int i=0;i<po.size();i++){
                     
                     JSONObject u=po.getJSONObject(i);
                    
                     System.out.println(u.get("step"));
                 }

Generally speaking, there are only two points in the analysis process:

First, the curly bracket is followed by JSONObject

Second, the one before the bracket is regarded as JSONArray

The results are as follows:

Posted by jdorma0 on Wed, 01 Apr 2020 07:14:25 -0700