如何从Json中提取值

时间:2022-03-02 16:00:10

I am getting a response String from server like below

我从服务器获得如下所示的响应字符串

{
  "name": "Json",
  "detail": {
    "first_name": "Json",
    "last_name": "Scott",
    "age": "23"
  },
  "status": "success"
}

I want to get the value of First name. How can I do that? Thanks in Advance.

我想知道名字的价值。我怎么做呢?提前谢谢。

4 个解决方案

#1


10  

see this code what i am used in my application

请查看我在应用程序中使用的代码

String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";

I retrieved like this

我检索

JSONObject json = (JSONObject) JSONSerializer.toJSON(data);        
    double coolness = json.getDouble( "coolness" );
    int altitude = json.getInt( "altitude" );
    JSONObject pilot = json.getJSONObject("pilot");
    String firstName = pilot.getString("firstName");
    String lastName = pilot.getString("lastName");

    System.out.println( "Coolness: " + coolness );
    System.out.println( "Altitude: " + altitude );
    System.out.println( "Pilot: " + lastName );

#2


9  

Use a JSON parser. There are plenty of JSON parsers written in Java.

使用JSON解析器。有很多用Java编写的JSON解析器。

http://www.json.org/

http://www.json.org/

Look under the Java section and find one you like.

查看Java部分,找到一个您喜欢的。

#3


9  

Pasting my code here, this should help. It shows the package which can be used.

在这里粘贴我的代码,这应该会有帮助。它显示了可以使用的包。

import org.json.JSONException;
import org.json.JSONObject;

public class extractingJSON {

    public static void main(String[] args) throws JSONException {
        // TODO Auto-generated method stub

        String jsonStr = "{\"name\":\"SK\",\"arr\":{\"a\":\"1\",\"b\":\"2\"}}";
        JSONObject jsonObj = new JSONObject(jsonStr);
        String name = jsonObj.getString("name");
        System.out.println(name);

        String first = jsonObj.getJSONObject("arr").getString("a");
        System.out.println(first);

    }

}

#4


0  

If you don't mind adding a dependency, you can use JsonPath.

如果您不介意添加依赖项,您可以使用JsonPath。

import com.jayway.jsonpath.JsonPath;

String firstName = JsonPath.read(rawJsonString, "$.detail.first_name");

"$" specifies the root of the raw json string and then you just specify the path to the field you want. This will always return a string. You'll have to do any casting yourself.

“$”指定原始json字符串的根,然后指定到所需字段的路径。这将总是返回一个字符串。你得自己选。

Be aware that it'll throw a PathNotFoundException at runtime if the path you specify doesn't exist.

注意,如果指定的路径不存在,它将在运行时抛出PathNotFoundException。

#1


10  

see this code what i am used in my application

请查看我在应用程序中使用的代码

String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";

I retrieved like this

我检索

JSONObject json = (JSONObject) JSONSerializer.toJSON(data);        
    double coolness = json.getDouble( "coolness" );
    int altitude = json.getInt( "altitude" );
    JSONObject pilot = json.getJSONObject("pilot");
    String firstName = pilot.getString("firstName");
    String lastName = pilot.getString("lastName");

    System.out.println( "Coolness: " + coolness );
    System.out.println( "Altitude: " + altitude );
    System.out.println( "Pilot: " + lastName );

#2


9  

Use a JSON parser. There are plenty of JSON parsers written in Java.

使用JSON解析器。有很多用Java编写的JSON解析器。

http://www.json.org/

http://www.json.org/

Look under the Java section and find one you like.

查看Java部分,找到一个您喜欢的。

#3


9  

Pasting my code here, this should help. It shows the package which can be used.

在这里粘贴我的代码,这应该会有帮助。它显示了可以使用的包。

import org.json.JSONException;
import org.json.JSONObject;

public class extractingJSON {

    public static void main(String[] args) throws JSONException {
        // TODO Auto-generated method stub

        String jsonStr = "{\"name\":\"SK\",\"arr\":{\"a\":\"1\",\"b\":\"2\"}}";
        JSONObject jsonObj = new JSONObject(jsonStr);
        String name = jsonObj.getString("name");
        System.out.println(name);

        String first = jsonObj.getJSONObject("arr").getString("a");
        System.out.println(first);

    }

}

#4


0  

If you don't mind adding a dependency, you can use JsonPath.

如果您不介意添加依赖项,您可以使用JsonPath。

import com.jayway.jsonpath.JsonPath;

String firstName = JsonPath.read(rawJsonString, "$.detail.first_name");

"$" specifies the root of the raw json string and then you just specify the path to the field you want. This will always return a string. You'll have to do any casting yourself.

“$”指定原始json字符串的根,然后指定到所需字段的路径。这将总是返回一个字符串。你得自己选。

Be aware that it'll throw a PathNotFoundException at runtime if the path you specify doesn't exist.

注意,如果指定的路径不存在,它将在运行时抛出PathNotFoundException。