user3251123 user3251123 - 4 months ago 10
JSON Question

Hi, I need help parse through a JSON file in Java

I am trying to parse through a JSON file using the library JSON simple. The first two string are good. However, when I try to parse through object social I get facebook: null, Pinterest : null, and rss: null . How do I parse through my second object

Here is my JSON file

"blogURL": "www.sheriyBegin",
"twitter": "",
"social": {
"facebook": "",
"pinterest": "",
"rss": ""

Here is the code I wrote

package javaugh;


import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JavaUgh {

public static void main(String[] args)throws Exception{
JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader("C:\\Users\\HP\\Desktop\\Research\\file2.txt"));
JSONObject jsonObject = (JSONObject) obj;

String blog = (String)jsonObject.get("blogURL");
String twitter = (String)jsonObject.get("twitter");

JSONObject jsonObject2 = (JSONObject) obj;
String face = (String)jsonObject2.get("facebook");
String pin = (String)jsonObject2.get("pinterest");
String rss = (String)jsonObject2.get("rss");

System.out.println("Blog: "+ blog);
System.out.println("Twitter Page : " + twitter);
System.out.println("Facebook Page : " + face);
System.out.println("Pintersect: " + pin);
System.out.println("Rss : " + rss);




Blog: www.sheriyBegin
Twitter Page :
Facebook Page : null
Pintersect: null
Rss : null


You should obtain the second json object from the first one, not by creating a new reference to the first one.

JSONObject jsonObject2 = (JSONObject) jsonObject.get("social");