我从服务器获取到一个类似下面这样的响应字符串:
{
"name": "Json",
"detail": {
"first_name": "Json",
"last_name": "Scott",
"age": "23"
},
"status": "success"
}
我想获取“First name”的值。我该怎么做?
看看这段代码,这是我在我的应用程序中使用的
String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";
我像这样检索
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 );
将我的代码粘贴在这里,这应该会有所帮助。它显示可用的软件包。
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);
}
}
getString()
方法来获取特定键的字符串值。如果这个答案对你有帮助,请使用答案左侧的复选框接受它。 - Jasarienimport com.jayway.jsonpath.JsonPath;
String firstName = JsonPath.read(rawJsonString, "$.detail.first_name");
"$"
指定了原始json字符串的根,然后您只需指定要获取的字段的路径。这将始终返回一个字符串。您需要自己进行任何类型转换。请注意,如果您指定的路径不存在,它会在运行时抛出PathNotFoundException异常。
"String jsonErrorString=((HttpClientErrorException)exception).getResponseBodyAsString();
JSONObject jsonObj=null;
String errorDetails=null;
String status=null;
try {
jsonObj = new JSONObject(jsonErrorString);
int index =jsonObj.getString("detail").indexOf(":");
errorDetails=jsonObj.getString("detail").substring(index);
status=jsonObj.getString("status");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
item.put("status", status);
item.put("errordetailMsg", errorDetails);
如果你收到一个类似于json格式的 response
字符串,你可以这样做:
import org.json.simple.parser.JSONParser;
JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(response);
然后获取 name
:
String name = (String) jsonObject.get("name");
获取 firstname
:
JSONObject detailObject = (JSONObject) jsonObject.get("detail");
String firstname = (String) detailObject.get("first_name");
JSONArray ja = new JSONArray(json);
JSONObject ob = ja.getJSONObject(0);
String nh = ob.getString("status");
[ { "status" : "true" } ]
这里的 'json' 是一个字符串,状态是我将从中获取值的键
我们可以使用以下代码从JSON对象中获取键名字符串
JsonObject json = new JsonObject();
json.get("key").getAsString();
这将返回不带字符串中的双引号" "
的字符串
//import java.util.ArrayList;
//import org.bson.Document;
Document root = Document.parse("{\n"
+ " \"name\": \"Json\",\n"
+ " \"detail\": {\n"
+ " \"first_name\": \"Json\",\n"
+ " \"last_name\": \"Scott\",\n"
+ " \"age\": \"23\"\n"
+ " },\n"
+ " \"status\": \"success\"\n"
+ "}");
System.out.println(((String) root.get("name")));
System.out.println(((String) ((Document) root.get("detail")).get("first_name")));
System.out.println(((String) ((Document) root.get("detail")).get("last_name")));
System.out.println(((String) ((Document) root.get("detail")).get("age")));
System.out.println(((String) root.get("status")));
final String json = "{\"name\": \"asdf\", \"age\": \"23\", }";
final JsonNode node = new ObjectMapper().readValue(json, JsonNode.class);
System.out.println(jsonNode.findValuesAsText("name"));
如果您想将JSON对象存储为Map,则可以使用ObjectNode(它是JsonNode的Map实现)。 您可以按键获取元素,如下所示。
final ObjectNode node = new ObjectMapper().readValue(json, ObjectNode.class);
if (node.has("name")) {
System.out.println("name: " + node.get("name"));
}
final ObjectNode node = new ObjectMapper().readValue(json, ObjectNode.class);
System.out.println("name: " + node.get(0));