如何使用json-simple从Json解析为java

时间:2015-03-27 15:55:19

标签: java json parsing

我是编程新手,我希望得到你的帮助。

我想解析这个JSON代码:

{
bugs: [1]
0:  {
cf_status_firefox_esr31: "---"
cf_status_b2g_2_1_s: "---"
blocks: [0]
is_cc_accessible: true
cf_tracking_b2g: "---"
keywords: [0]
cf_qa_whiteboard: ""
url: "http://www.cars.com"
qa_contact: ""
cf_status_firefox38: "---"
cf_fx_iteration: "---"
cf_tracking_firefox_relnote: "---"
cf_last_resolved: null
cf_fx_points: "---"
cc_detail: [3]
0:  {
email: "clivegpage@googlemail.com"
real_name: "Clive Page"
name: "clivegpage@googlemail.com"
id: 315313
}-
1:  {
email: "steevo@gmx.co.uk"
real_name: "Steve"
name: "steevo@gmx.co.uk"
id: 41878
}-
2:  {
email: "tdowner@mozilla.com"
real_name: "Tyler Downer [:Tyler]"
name: "tdowner@mozilla.com"
id: 310130
}-
-
summary: "print paginates incorrectly, matches print preview but is defective."
cf_user_story: ""
platform: "x86"
cf_tracking_firefox38: "---"
cf_tracking_e10s: "---"
version: "1.9.2 Branch"
cf_blocking_fx: "---"
mentors_detail: [0]
cf_feature_b2g: "---"
cf_tracking_firefox39: "---"
is_creator_accessible: true
is_confirmed: false
priority: "--"
cf_tracking_firefox37: "---"
cf_status_firefox39: "---"
assigned_to_detail: {
email: "nobody@mozilla.org"
real_name: "Nobody; OK to take it and work on it"
name: "nobody@mozilla.org"
id: 1
}-
creator: "steevo@gmx.co.uk"
cf_blocking_b2g: "---"
last_change_time: "2010-04-22T04:47:56Z"
cf_status_firefox36: "---"
cf_tracking_firefox_esr31: "---"
cf_status_b2g_2_2: "---"
creator_detail: {
email: "steevo@gmx.co.uk"
real_name: "Steve"
name: "steevo@gmx.co.uk"
id: 41878
}-
cc: [3]
0:  "clivegpage@googlemail.com"
1:  "steevo@gmx.co.uk"
2:  "tdowner@mozilla.com"
-
assigned_to: "nobody@mozilla.org"
groups: [0]
see_also: [0]
id: 384841
whiteboard: ""
creation_time: "2007-06-18T04:32:52Z"
depends_on: [0]
cf_tracking_firefox_esr38: "---"
cf_status_firefox_esr38: "---"
cf_status_b2g_2_1: "---"
mentors: [0]
dupe_of: null
resolution: ""
classification: "Components"
alias: null
op_sys: "Windows XP"
cf_blocking_fennec: "---"
status: "UNCONFIRMED"
cf_status_b2g_2_0: "---"
cf_tracking_firefox36: "---"
cf_status_b2g_2_0m: "---"
cf_crash_signature: ""
is_open: true
severity: "normal"
cf_tracking_relnote_b2g: "---"
flags: [0]
cf_status_b2g_1_4: "---"
cf_status_firefox37: "---"
cf_status_b2g_3_0: "---"
component: "Print Preview"
target_milestone: "---"
product: "Core"
}-
-
}

到java,然后使用我将从每个bug中获取的信息。例如:

id=.., summary=.., is_confirmed=... 

等等。

这是我的代码:

package bugzilla;

import java.net.URL;
import static java.nio.file.Files.list;
import static java.rmi.Naming.list;
import java.util.ArrayList;
import static java.util.Collections.list;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ContainerFactory;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class JsonParseTest {


ArrayList<UrlConnection> list= Bugzilla.getConnectionList();

public void doTheDecoding() throws ParseException{


    //creating the Parser
    JSONParser jsonparser= new JSONParser();

    for(int i=0; i<list.size();i++){

    // the string text is the json code

    String text=list.get(i).getSb().toString();
    JSONObject jsonObject=(JSONObject)jsonparser.parse(text);

             JSONArray array= (JSONArray) jsonObject.get("bugs");

             for (int j=0; j<array.size(); j++){
                 System.out.println(array.get(j));
             }

            }


}


}
运行后

我的输出是:

run:
The url is valid!
-----------------With url info------------
 query: id=384841
 path: /rest/bug
 authority: bugzilla.mozilla.org
 file: /rest/bug?id=384841
 host: bugzilla.mozilla.org
 protocol: https
 port: -1
 info: null
-------------------------------------------
{"bugs":[{"cf_status_firefox_esr31":"---","cf_status_b2g_2_1_s":"---","blocks":[],"is_cc_accessible":true,"cf_tracking_b2g":"---","keywords":[],"cf_qa_whiteboard":"","url":"http://www.cars.com","qa_contact":"","cf_status_firefox38":"---","cf_fx_iteration":"---","cf_tracking_firefox_relnote":"---","cf_last_resolved":null,"cf_fx_points":"---","cc_detail":[{"email":"clivegpage@googlemail.com","real_name":"Clive Page","name":"clivegpage@googlemail.com","id":315313},{"email":"steevo@gmx.co.uk","real_name":"Steve","name":"steevo@gmx.co.uk","id":41878},{"email":"tdowner@mozilla.com","real_name":"Tyler Downer [:Tyler]","name":"tdowner@mozilla.com","id":310130}],"summary":"print paginates incorrectly, matches print preview but is defective.","cf_user_story":"","platform":"x86","cf_tracking_firefox38":"---","cf_tracking_e10s":"---","version":"1.9.2 Branch","cf_blocking_fx":"---","mentors_detail":[],"cf_feature_b2g":"---","cf_tracking_firefox39":"---","is_creator_accessible":true,"is_confirmed":false,"priority":"--","cf_tracking_firefox37":"---","cf_status_firefox39":"---","assigned_to_detail":{"email":"nobody@mozilla.org","real_name":"Nobody; OK to take it and work on it","name":"nobody@mozilla.org","id":1},"creator":"steevo@gmx.co.uk","cf_blocking_b2g":"---","last_change_time":"2010-04-22T04:47:56Z","cf_status_firefox36":"---","cf_tracking_firefox_esr31":"---","cf_status_b2g_2_2":"---","creator_detail":{"email":"steevo@gmx.co.uk","real_name":"Steve","name":"steevo@gmx.co.uk","id":41878},"cc":["clivegpage@googlemail.com","steevo@gmx.co.uk","tdowner@mozilla.com"],"assigned_to":"nobody@mozilla.org","groups":[],"see_also":[],"id":384841,"whiteboard":"","creation_time":"2007-06-18T04:32:52Z","depends_on":[],"cf_tracking_firefox_esr38":"---","cf_status_firefox_esr38":"---","cf_status_b2g_2_1":"---","mentors":[],"dupe_of":null,"resolution":"","classification":"Components","alias":null,"op_sys":"Windows XP","cf_blocking_fennec":"---","status":"UNCONFIRMED","cf_status_b2g_2_0":"---","cf_tracking_firefox36":"---","cf_status_b2g_2_0m":"---","cf_crash_signature":"","is_open":true,"severity":"normal","cf_tracking_relnote_b2g":"---","flags":[],"cf_status_b2g_1_4":"---","cf_status_firefox37":"---","cf_status_b2g_3_0":"---","component":"Print Preview","target_milestone":"---","product":"Core"}]}

{"creation_time":"2007-06-18T04:32:52Z","keywords":[],"see_also":[],"resolution":"","cf_blocking_fx":"---","cf_fx_iteration":"---","cf_blocking_b2g":"---","cf_status_b2g_1_4":"---","id":384841,"cf_status_b2g_3_0":"---","dupe_of":null,"is_open":true,"cf_qa_whiteboard":"","cf_status_firefox39":"---","cf_status_firefox38":"---","assigned_to_detail":{"name":"nobody@mozilla.org","real_name":"Nobody; OK to take it and work on it","id":1,"email":"nobody@mozilla.org"},"creator_detail":{"name":"steevo@gmx.co.uk","real_name":"Steve","id":41878,"email":"steevo@gmx.co.uk"},"cf_status_firefox37":"---","priority":"--","cf_status_firefox36":"---","classification":"Components","version":"1.9.2 Branch","op_sys":"Windows XP","last_change_time":"2010-04-22T04:47:56Z","cf_crash_signature":"","mentors_detail":[],"cf_tracking_b2g":"---","cf_status_b2g_2_0m":"---","status":"UNCONFIRMED","cf_feature_b2g":"---","cf_tracking_firefox39":"---","cf_tracking_firefox38":"---","is_cc_accessible":true,"cf_tracking_firefox37":"---","cf_tracking_firefox36":"---","cf_tracking_firefox_esr38":"---","cf_tracking_relnote_b2g":"---","flags":[],"cf_status_firefox_esr31":"---","whiteboard":"","platform":"x86","cf_tracking_firefox_esr31":"---","cf_blocking_fennec":"---","cf_status_b2g_2_1_s":"---","cf_status_firefox_esr38":"---","cf_fx_points":"---","alias":null,"cf_status_b2g_2_2":"---","cf_status_b2g_2_1":"---","cf_status_b2g_2_0":"---","assigned_to":"nobody@mozilla.org","summary":"print paginates incorrectly, matches print preview but is defective.","cf_tracking_e10s":"---","cc":["clivegpage@googlemail.com","steevo@gmx.co.uk","tdowner@mozilla.com"],"severity":"normal","creator":"steevo@gmx.co.uk","product":"Core","depends_on":[],"cf_tracking_firefox_relnote":"---","blocks":[],"is_confirmed":false,"groups":[],"cf_last_resolved":null,"url":"http:\/\/www.cars.com","qa_contact":"","target_milestone":"---","component":"Print Preview","is_creator_accessible":true,"cc_detail":[{"name":"clivegpage@googlemail.com","real_name":"Clive Page","id":315313,"email":"clivegpage@googlemail.com"},{"name":"steevo@gmx.co.uk","real_name":"Steve","id":41878,"email":"steevo@gmx.co.uk"},{"name":"tdowner@mozilla.com","real_name":"Tyler Downer [:Tyler]","id":310130,"email":"tdowner@mozilla.com"}],"mentors":[],"cf_user_story":""}
BUILD SUCCESSFUL (total time: 1 second)

我的问题是如何从这个json中获取特定字段。 例如,取is_open:true字段。

提前致谢!

0 个答案:

没有答案