json-lib解析json数据import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.json.JSONObject;public class Getjson {\x05public void main(String []args)\x05{\x05\x05 String json = "{'name':'亲亲宝宝','array':[{'a':'111','b':'

来源:学生作业帮助网 编辑:作业帮 时间:2024/10/08 17:58:10
json-lib解析json数据import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.json.JSONObject;public class Getjson {\x05public void main(String []args)\x05{\x05\x05 String json =
xUOPWnfnvA1Am+[Wۋ& D1g>! 3]s?{۵Ls_s׶7o_.>v1Mzs*ѫS3wno-0X0 9e {mbbG{K7QۚE-ݲs3صyT'.ԏ܈ht[oJE N¥(:TjE Un1|j0A0/uIZ:t9}?/hT*)H.R.y~FrTIA $n11G\`^1bE"u̖cfH0*5ۓS,{lE:6;Y^% Uz8x(&E?MIq}HL.ȂQ URf.B!^*J )Ȃ;CKG|@?c>E~=dŠ ֍w] UӭgG.nǧٓd|{/.v{.Ix9|X~REg߷^v`nxF0@MAZ! ݏ=L3IB]3S!|smwWv.vӕ\^&#pNZP6"dP]))̹ƢuJV ImHhĒŐ,И5灔Ш"y YqZwoL%9i4XRJ*/3ԗJeqq=܃NDC'g*-*ayrNyX$.PD61;8L2}ThY`~~C"&+cI4L1~TF"ҫL*eDG-٠ ~['֤w#8m9=IO

json-lib解析json数据import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.json.JSONObject;public class Getjson {\x05public void main(String []args)\x05{\x05\x05 String json = "{'name':'亲亲宝宝','array':[{'a':'111','b':'
json-lib解析json数据
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class Getjson
{
\x05public void main(String []args)
\x05{
\x05\x05 String json = "{'name':'亲亲宝宝','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'亲亲宝宝'}";
\x05\x05 try
\x05\x05 {
\x05\x05 \x05JSONObject jsonObject = JSONObject.fromObject(json);
\x05\x05 \x05String name = jsonObject.getString("name");
\x05\x05 \x05String address = jsonObject.getString("address");
\x05\x05 \x05System.out.println("name is:" + name);
\x05\x05 \x05System.out.println("address is:" + address);
\x05\x05 \x05JSONArray jsonArray = jsonObject.getJSONArray("array");
\x05\x05 \x05for (int i = 0; i < jsonArray.size(); i++)
\x05\x05 \x05{
\x05\x05 \x05 System.out.println("item" + i + ":" + jsonArray.getString(i));
\x05\x05 \x05}
\x05\x05 } catch (JSONException e)
\x05\x05 {
\x05\x05 e.printStackTrace();
\x05\x05 }
}
}
我想知道为什么用json-lib解析上面的json数据没有反映.

json-lib解析json数据import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.json.JSONObject;public class Getjson {\x05public void main(String []args)\x05{\x05\x05 String json = "{'name':'亲亲宝宝','array':[{'a':'111','b':'
public class GetJson {
\x05public static void main(String[] args) {//main方法写错了哈.掉了static
\x05\x05String json = "{'name': '亲亲宝宝','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'亲亲宝宝'}";
\x05\x05try {
\x05\x05\x05JSONObject jsonObject = JSONObject.fromObject(json);
\x05\x05\x05String name = jsonObject.getString("name");
\x05\x05\x05String address = jsonObject.getString("address");
\x05\x05\x05System.out.println("name is:" + name);
\x05\x05\x05System.out.println("address is:" + address);
\x05\x05\x05JSONArray jsonArray = jsonObject.getJSONArray("array");
\x05\x05\x05for (int i = 0; i < jsonArray.size(); i++) {
\x05\x05\x05\x05System.out.println("item" + i + ":" + jsonArray.getString(i));
\x05\x05\x05}
\x05\x05} catch (JSONException e) {
\x05\x05\x05e.printStackTrace();
\x05\x05}
\x05}
}
修改了main方法就可以了