Here is my code. It is an attempt to read JSON using the GSON library.
JsonReader reader = new JsonReader( new BufferedReader(new InputStreamReader(s3Object.getObjectContent())) );
reader.beginArray();
int gsonVal = 0;
while (reader.hasNext()) {
JsonParser _parser = new JsonParser();
JsonElement jsonElement = _parser.parse(reader);
JsonObject jsonObject1 = jsonElement.getAsJsonObject();
}
Now, I need to add 2 additional fields to this JsonObject
. I need to add primary_key
field and hash_index
field.
I tried the below, but it didn't work.
jsonObject1.add("hash_index", hashIndex.toString().trim());
jsonObject1.add("primary_key", i);
When these two values are added, the JsonObject will look like below.
{
"hash_index": "00102x05h06l0aj0dw",
"body": "Who's signing up for Obamacare?",
"_type": "ArticleItem",
"title": "Who's signing up for Obamacare? - Jan. 13, 2014",
"source": "money.cnn.com",
"primary_key": 0,
"last_crawl_date": "2014-01-14",
"url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html"
}
How can I do this? This is my first time with GSON.
You have to use the JsonObject#addProperty()
final JsonObject json = new JsonObject();
json.addProperty("type", "ATP");
In Your case
jsonObject1.addProperty("hash_index", hashIndex.toString().trim());
jsonObject1.addProperty("primary_key", i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With