Parsing nested JSON array in android - json

I am trying to parse this json in android and displaying the contents in list.
I am able to retrieve title,location and creator but dont know how to get inside des_update and retrieve text on '0'
[
{
"title": "my event ",
"location": "Chennai, Tamil Nadu, India",
"creator": "abc",
"des_update": {
"0": "this is my event with moderator"
}
},
{
"title": "my event 17",
"location": "pune",
"creator": "abc",
"des_update": {}
}
]
this is my parsing code I want to display data in alist
// convert json string to json array
JSONArray aJson = new JSONArray(sJson);
// create apps list
List<events> apps = new ArrayList<events>();
for(int i=0; i<aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
events app = new events();
app.setTitle(json.getString("title"));
app.setDate(json.getString("date"));
app.setLocation(json.getString("location"));
// add the app to apps list
apps.add(app);
...
}

you can use the following code:
JSONArray jsonArray = new JSONArray(json);
JSONObject object = jsonArray.getJSONObject(0);
JSONObject desObject = object.getJSONObject("des_update");
String data = desObject.getString("0");
System.out.println(data);
Here first you are loading your json string into JSONArray, and then you are taking one object from JSONArray instance. As des_update is an object with in the object you need to get that object calling getJSONObject() by passing your key and from there you can load the data that you want to use.

JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject object = jsonArray.getJSONObject(i);
String title = object.getString("title");
String location = object.getString("location");
String creator = object.getString("creator");
JSONObject des_update = object.getJSONObject("des_update");
String str = des_update.getString("0");
System.out.println("des_update-------" + str);
}

Related

serialize a json using gson

I am using com.google.gson.JsonObject to send the json inside 'parameters' to a rest endpoint.
{
"parameters":
"{
\"customer\" : {
\"firstName\": \"Temp\",
\"lastName\": \"Temp\",
\"emailAddresses\": [\"temp1#temp.com\"],
\"address\": {
\"street1\": \"123 W Temp St\",
\"city\": \"Temp\",
\"state\": \"Illinois\",
\"zipCode\": \"61122\"
}
},
\"options\" : [\"tv\"]
}"
}
Since Parameters is a json string, I am trying to do this :
JsonObject json = new JsonObject();
json.addProperty("options", ??);
I am not sure how to do it for customer and options.
'options' is a java Set, whereas customer is an object.
You may use:
Gson gson = new Gson();
String json = "{\"parameters\": {\"customer\" ... ";
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
JsonArray options = new JsonArray();
options.add("tv");
jsonObject.add("options", options);
String output = gson.toJson(jsonObject);

creating json as is shown in Postman

I'm trying to post data via API to InsightVM. I keep getting error 400 which means wrong format. I break it down, the problem is in hostnames part.
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
if (host_info.getDataType() == dataType.Web_Vulnerability && methodType == MethodType.Add_host_To_Site)
{
JSONObject json2 = new JSONObject();
json2.put("name",host_info.getHost_fqdn());
json2.put("source", "Splunk");
array.add(json2);
DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
json.put("date",OffsetDateTime.now().format(dtf) );
json.put("ip",host_info.getHost_ip());
json.put("os",host_info.getOperating_system());
json.put("hostNames",array); // problem is here
}
is the code above provide the same format as below:
{
"date": "2019-05-20",
"ip": "00.00.00.00",
"os":"Linux",
"hostNames":
[
{
"name": "corporate-workstation-1102DC.acme.com",
"source": "Splunk"
}
]
}
JSONArray implements Collection.
While JSONObject has an overloaded put() method,
which takes a Collection and wraps it in a JSONArray which might be causing the problem.
Can you try the following -
jsonObject.put("hostNames",(Object)array);

JSON Object Exception

I am writing a web service-REST.
I am receiving a text_plain format.
Example of my data:
{"data_log":
{
"methodClass": [{"methodName": 1, "methodStatus": "1"},
{"methodName": 2, "methodStatus": "1"}]
}
}
In my Restful Webservice, I try to read the data, but I got error 500 Internal Server Error-JSON Object not found.
public int adaptiveAuth(String objDataLog){
logWriter("objDataLog:"+objDataLog);
JSONObject obj = new JSONObject(objDataLog);
JSONArray methodClassObj=(JSONArray)obj.get("methodClass");
if (methodClassObj != null) {
JSONObject methodObj;
String entrymethodName, entrymethodStatus;
for (Object o : methodClassObj) {
methodObj = (JSONObject) o;
entrymethodName = String.valueOf(methodObj.get("methodName"));
entrymethodStatus = String.valueOf(methodObj.get("methodStatus"));
logWriter("entrymethodName:"+entrymethodName);
logWriter("entrymethodStatus:"+entrymethodStatus);
}
}
}
I already tried to use the below code, but it still give me the error.
String methodClass= obj.getJSONObject("data_log").getString("methodClass");
I'm expecting to put the current data into an 2 dimension array which it would be like this:
[1,1],[2,1]
Anyone could give me some suggestion on how to solve this issue?
**Try this code for parsing json data**
ArrayList<HashMap<String, String>> list;
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray b= jsonObj.getJSONArray("methodClass");
// looping through All data
for (int i = 0; i < b.length(); i++) {
JSONObject c = b.getJSONObject(i);
String entrymethodName = c.getJSONObject("methodClass").getString("methodName");
String entrymethodStatus = c.getJSONObject("methodClass").getString("methodStatus");
// tmp hash map for storing values
HashMap<String, String> co = new HashMap<>();
// adding each child node to HashMap key => value
co.put(entrymethodName, entrymethodStatus );
// adding co to list
list.add(co);
Also check this - https://gist.github.com/codebutler/2339666
I changed by String to this format:
{ "methodSet": [
{
"methodName": 1,
"methodStatus": "1"
},
{
"methodName": 2,
"methodStatus": "1"
}
]
}
and for the code :
ArrayList<String> listMethod = new ArrayList<String>();
JSONArray arr=(JSONArray)obj.get("methodClass");
if (arr != null) {
JSONObject objMethod;
String MethodName, MethodStatus;
for (Object o : arr) {
objMethod = (JSONObject) o;
MethodName = String.valueOf(objMethod.get("methodName"));
MethodStatus = String.valueOf(objMethod.get("methodStatus"));
int resultMethodName = Integer.parseInt(MethodName);
int resultMethodStatus = Integer.parseInt(MethodStatus);
logWriter("MethodStatus"+resultMethodName);
logWriter("MethodName"+resultMethodStatus);
listMethod.add("(" + MethodName + "," + MethodStatus + ")");
logWriter("listMethod is : "+listMethod);
}
}

get particular value from json object

{
"CustomerDetails": [
{
"AccountName": "test",
"DnBNumber": 12342314
}
]
}
I want to get value from this json object.
I tried below but not working.
jsonObject.getJSONObject("AccountName").toString();
In the JSON you put, there are a JsonObject that contains a JsonArray, that contains two String, so..
final JSONObject jsonObject = new JSONObject(YOUR_JSON);
final JSONArray jsonArray = obj.getJSONArray("CustomerDetails");
for (int i = 0; i < geodata.length(); ++i) {
final JSONObject detail = geodata.getJSONObject(i);
System.out.println(detail.getString("AccountName"));
System.out.println(detail.getString("DnBNumber"));
}

how to add json object to a json element in spring

i want to make a json object where it looks like
{
"version": "1.0",
"message": {
"ID": "test",
"Text": "Acd"
}
}
i need to post this data to server i am trying to add a json object to a json object how to do it
org.json.JSONObject json = new org.json.JSONObject();
org.json.JSONObject root = new org.json.JSONObject();
root.put("version", "1.0");
json.put("ID", "test");
json.put("Text", "acd");
root.put("message", json);
i am getting root.put is not a method for(string,jsonobject)
Create a Map for nested json object, like this:
Map json = new HashMap();
org.json.JSONObject root = new org.json.JSONObject();
root.put("version", "1.0");
json.put("ID", "test");
json.put("Text", "acd");
root.put("message", json);
According to doc:
Put a key/value pair in the JSONObject, where the value will be a
JSONObject which is produced from a Map.