如何在Java程序中从JSON获取值?

如何在Java程序中从JSON获取值?,java,json,Java,Json,我是JSON的初学者 如何从Java程序中的以下JSON对象获取“3037904”的值 {“查询”:{“页面”:{“3037904”:{“页面ID”:3037904,“ns”:0,“标题”:“凯宾斯基”, “类别”:[{“ns”:14,“标题”:“类别:1897年成立的公司”},{“ns”:14,“标题”:“类别:连锁酒店”},{“ns”:14,“标题”:“类别:凯宾斯基酒店”}]}} 我试着 JSONObject query = json.getJSONObject("query"); int

我是JSON的初学者

如何从Java程序中的以下JSON对象获取“3037904”的值

{“查询”:{“页面”:{“3037904”:{“页面ID”:3037904,“ns”:0,“标题”:“凯宾斯基”, “类别”:[{“ns”:14,“标题”:“类别:1897年成立的公司”},{“ns”:14,“标题”:“类别:连锁酒店”},{“ns”:14,“标题”:“类别:凯宾斯基酒店”}]}}

我试着

JSONObject query = json.getJSONObject("query");
int pages = query.getInt("pages");
但这需要时间

"{"3037904":{"pageid":3037904,"ns":0,"title":"Kempinski",
"categories":[{"ns":14,"title":"Category:Companies established in 1897"},{"ns":14,"title":"Category:Hotel chains"},{"ns":14,"title":"Category:Kempinski Hotels"}]}}}}", 

不仅是“3037904”

看看GSON库:

以下是一些很好的例子:


请看一下GSON库:

以下是一些很好的例子:


您需要对
JSONObject
做更多的工作

在这个答案中,我假设您希望获得该
pageid

让我们仅假设嵌套中存在
pageid
——在特定级别:

// "query" is the top-level object: 
JSONObject query = json.getJSONObject("query");
// "pages" is a field of "query"
JSONObject pages = query.getJSONObject("pages");

// these will hold the object with the value that you want, and that value:
JSONObject nestedObject = null;
int pageId = 0;

// these are the property names in the "pages" object:
String[] keys = pages.getNames(pages);

// iterate over the keys in the "pages" object, looks for JSONObjects:
for (int i = 0; i < keys.length; i++)
{
    try
    {
        nestedObject = pages.getJSONObject(keys[i]);
        // only consider objects with a "pageid" key, stop at the first one:
        if (nestedObject.has("pageid"))
            break;
    }
    catch (JSONException je)
    { ; }
}

if (nestedObject != null)
   pageId = nestedObject.getInt("pageid");
/“query”是顶级对象:
JSONObject query=json.getJSONObject(“查询”);
//“页面”是“查询”字段
JSONObject pages=query.getJSONObject(“页面”);
//这些将保存具有所需值的对象,以及该值:
JSONObject nestedObject=null;
int pageId=0;
//以下是“pages”对象中的属性名称:
String[]keys=pages.getNames(pages);
//迭代“pages”对象中的键,查找JSONObject:
for(int i=0;i

您的JSON输入看起来很奇怪,因为第一个嵌套对象有一个
pages
字段,其中包含另一个对象。复数形式的名称,
pages
,以及嵌套对象(将包含对象的键复制为对象内的
pageid
键),表明
pages
应该是多个此类对象的数组。

您需要对
JSONObject
做更多的工作

在这个答案中,我假设您希望获得该
pageid

让我们仅假设嵌套中存在
pageid
——在特定级别:

// "query" is the top-level object: 
JSONObject query = json.getJSONObject("query");
// "pages" is a field of "query"
JSONObject pages = query.getJSONObject("pages");

// these will hold the object with the value that you want, and that value:
JSONObject nestedObject = null;
int pageId = 0;

// these are the property names in the "pages" object:
String[] keys = pages.getNames(pages);

// iterate over the keys in the "pages" object, looks for JSONObjects:
for (int i = 0; i < keys.length; i++)
{
    try
    {
        nestedObject = pages.getJSONObject(keys[i]);
        // only consider objects with a "pageid" key, stop at the first one:
        if (nestedObject.has("pageid"))
            break;
    }
    catch (JSONException je)
    { ; }
}

if (nestedObject != null)
   pageId = nestedObject.getInt("pageid");
/“query”是顶级对象:
JSONObject query=json.getJSONObject(“查询”);
//“页面”是“查询”字段
JSONObject pages=query.getJSONObject(“页面”);
//这些将保存具有所需值的对象,以及该值:
JSONObject nestedObject=null;
int pageId=0;
//以下是“pages”对象中的属性名称:
String[]keys=pages.getNames(pages);
//迭代“pages”对象中的键,查找JSONObject:
for(int i=0;i
您的JSON输入看起来很奇怪,因为第一个嵌套对象有一个
pages
字段,其中包含另一个对象。复数形式的名称,
pages
,以及嵌套对象(将包含对象的键复制为对象内的
pageid
键)表明
pages
应该是多个此类对象的数组