«

java操作Json数据

时间:2024-3-2 17:41     作者:韩俊     分类: Android


最近要使用java来操作Json数据,虽然Json用过几次,但一直没有好好总结,趁这次来总结一下java操作Json数据。Java操作Json有很多工具包,地址为http://www.json.org/,可以自己上去挑选。



这里我使用第一个org.json,最新源码地址为:https://github.com/douglascrockford/JSON-java,可以自己去下载,我也会在附件上传。下载源码后放进工程即可,包名为org.json

我们要操作的Json数据如下:

{"total":2,"class":"二年级","members":[{"age":18,"name":"李小红","score":95},{"age":28,"name":"王尼玛","score":85}]}

我们先用CreateJson()方法生成Json数据,然后把所有属性都解析出来。

package Myclass;


import org.json.JSONArray;

import org.json.JSONObject;


public class Test {


public static void main(String[] args) {  <br style="word-wrap:break-word">

    //解析Json--传入Json字符串  <br style="word-wrap:break-word">

    JSONObject my_json=new JSONObject(CreateJson());          <br style="word-wrap:break-word">

    int total=my_json.getInt(&quot;total&quot;);  <br style="word-wrap:break-word">

    System.out.println(&quot;总数为:&quot;&#43;total);       <br style="word-wrap:break-word">

      <br style="word-wrap:break-word">

    String class_name=my_json.getString(&quot;class&quot;);  <br style="word-wrap:break-word">

    System.out.println(&quot;班级为:&quot;&#43;class_name);  <br style="word-wrap:break-word">

      <br style="word-wrap:break-word">

    JSONArray members=my_json.getJSONArray(&quot;members&quot;);  <br style="word-wrap:break-word">

    for(int i=0;i&lt;members.length();i&#43;&#43;)  <br style="word-wrap:break-word">

    {  <br style="word-wrap:break-word">

        JSONObject member=(JSONObject)members.get(i);  <br style="word-wrap:break-word">

        System.out.println(&quot;姓名:&quot;&#43;member.getString(&quot;name&quot;)&#43;&quot; 年龄:&quot;&#43;member.getInt(&quot;age&quot;)  <br style="word-wrap:break-word">

                &#43;&quot; 成绩:&quot;&#43;member.getInt(&quot;score&quot;));  <br style="word-wrap:break-word">

    }  <br style="word-wrap:break-word">

      <br style="word-wrap:break-word">

}  <br style="word-wrap:break-word">

  <br style="word-wrap:break-word">

public static String CreateJson()  <br style="word-wrap:break-word">

{  <br style="word-wrap:break-word">

    JSONObject my_json=new JSONObject();  <br style="word-wrap:break-word">

    my_json.put(&quot;class&quot;, &quot;二年级&quot;);  <br style="word-wrap:break-word">

    my_json.put(&quot;total&quot;, 2);  <br style="word-wrap:break-word">

  <br style="word-wrap:break-word">

    JSONArray members=new JSONArray();  <br style="word-wrap:break-word">

    //第一个同学  <br style="word-wrap:break-word">

    JSONObject member1=new JSONObject();  <br style="word-wrap:break-word">

    member1.put(&quot;name&quot;, &quot;李小红&quot;);  <br style="word-wrap:break-word">

    member1.put(&quot;age&quot;, 18);  <br style="word-wrap:break-word">

    member1.put(&quot;score&quot;, 95);  <br style="word-wrap:break-word">

    members.put(member1);  <br style="word-wrap:break-word">

    //第二个同学  <br style="word-wrap:break-word">

    JSONObject member2=new JSONObject();  <br style="word-wrap:break-word">

    member2.put(&quot;name&quot;, &quot;王&#23612;玛&quot;);  <br style="word-wrap:break-word">

    member2.put(&quot;age&quot;, 28);  <br style="word-wrap:break-word">

    member2.put(&quot;score&quot;, 85);  <br style="word-wrap:break-word">

    members.put(member2);         <br style="word-wrap:break-word">

    my_json.put(&quot;members&quot;, members);  <br style="word-wrap:break-word">

      <br style="word-wrap:break-word">

    return my_json.toString();  <br style="word-wrap:break-word">

}  <br style="word-wrap:break-word">


}

复制代码
运行结果为:

标签: android

热门推荐