I am fetching data from the database as a JSON String:
{"companyName":"abcd","address":"abcdefg"}
How can I extract the company name from the given JSON String?
Refer JSON
JSONObject jsonObject = new JSONObject(YOUR_JSON_STRING);
JSONObject companyName = jsonObject .get("companyName");
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse("your string");
JsonObject jsonObj = jsonElement.getAsJsonObject();
String comapnyName = jsonObj.get("companyName").getAsString();
This is how we can parse json string in java. You will need to add com.google.gson library to compile this code.
JsonParser is the Java Standard Library. This code won't compile.JSONObject json = (JSONObject)new JSONParser().parse("{\"companyName\":\"abcd\", \"address\":\"abcdefg\"}");
System.out.println("companyName=" + json.get("companyName"));
System.out.println("address=" + json.get("address"));
how to work with JSON in Java? It seems unclear what you are asking about. I made this assumption only because ofjavatag.