android中的json二之json的读写

android中的json的读取

1.读取Json文件中的json内容

首先在将json文件放在目录assets下:

test.json

{"programmers":[{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
{"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
{"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
],
"authors":[
{"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"},
{"firstName":"Tad","lastName":"Williams","genre":"fantasy"},
{"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"}
],
"musicians":[
{"firstName":"Eric","lastName":"Clapton","instrument":"guitar"},
{"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"}
],
"cat":"test"}

在main.java中读取

 try {
            InputStreamReader isr=new InputStreamReader(getAssets().open("test.json"),"UTF-8");//打开test.json
            BufferedReader br=new BufferedReader(isr);//读取isr
            String line;
            StringBuilder builder=new StringBuilder();
            while ((line =br.readLine())!=null){
                builder.append(line);
            }
            br.close();
            isr.close();

            JSONObject root=new JSONObject(builder.toString());

            System.out.println(root.getString("cat"));
            JSONArray arry=root.getJSONArray("programmers");
            for (int i=0;i<arry.length();i++){
                JSONObject lan=arry.getJSONObject(i);
                System.out.println("----------------");
                System.out.println("firstName:"+lan.getString("firstName"));
                System.out.println("lastName:"+lan.getString("lastName"));
                System.out.println("email"+lan.getString("email"));
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("IO:" , e.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("JSON:", e.toString());
        }

显示结果:

技术分享

如要读取给定Json数据:

  可直接将值附上

//JSONObject root=new JSONObject(builder.toString());换成-》
            JSONObject root=new JSONObject("{'programmers':[{'firstName':'Brett','lastName':'McLaughlin','email':'aaaa'}]}");
2.创建JSON格式数据
 JSONObject root1=new JSONObject();
        
        try {

            root1.put("cat","it");

            JSONObject lan1=new JSONObject();
            lan1.put("id",1);
            lan1.put("ide","android");
            lan1.put("name","java");

            JSONObject lan2=new JSONObject();
            lan1.put("id",2);
            lan1.put("ide","xcod");
            lan1.put("name","ios");

            JSONObject lan3=new JSONObject();
            lan1.put("id",3);
            lan1.put("ide","vs");
            lan1.put("name","C#");

            JSONArray array=new JSONArray();
            array.put(lan1);
            array.put(lan2);
            array.put(lan3);

            root1.put("programmers",array);

            System.out.println(root1.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


显示结果:
技术分享

源码下载:http://download.csdn.net/detail/agonie201218/8657765

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。