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?
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.
JSONObject obj = new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
StringWriter out = new StringWriter();
obj.writeJSONString(out);
JSONObject json = (JSONObject)new JSONParser().parse("{\"companyName\":\"abcd\", \"address\":\"abcdefg\"}");
System.out.println("companyName=" + json.get("companyName"));
System.out.println("address=" + json.get("address"));