0

I want to read JSON array

"connectTo":[   
    {
        "url": "wss://localhost/opt/siml"
    }   
],

from JSON string :

{

"cluster":{
    "enabled":"true",
    "clusterName":"cluster0",
    "simlURL":"wss://localhost:5443/opt/siml"
},

"simlFieldWatchUrl":"fieldwatchholder.jsp",

"persistFolder":"clusterconfig/concentratorBPersist",

"sslCrtFile":"clusterconfig/certDirB/siml.crt",
"sslKeyFile":"clusterconfig/certDirB/siml.key",

"SIMLID":"TestServerB",

"localWebProxyServer":"localhost",
"localWebProxyPort":8080,
"SIMLProxyPort":8400,
"SIMLWebPort":8300,

"turnOnExtraSIMLWebSocket":"false",

"autoPromoteNewConnectionsFromPurgatory":true,

"connectTo":[   
    {
        "url": "wss://localhost/opt/siml"
    }   
],

"tempLogins":[
    {
        "username":"root",
        "password":"root"
    }
]

}

My code to read url is:

JSONArray connectTo = (JSONArray) config.get("connectTo");
                System.out.println("Connect to : " + connectTo);
                for (Object o : connectTo) {
                    JSONObject connect = (JSONObject) o;
                    String url = (String) connect.get("url");
                    System.out.println(url);
                }

But System.out.println("Connect to : " + connectTo); this is returning Connect to : []

I read some old question but did not get satisfactory answer. Please Help. And thank you in advance :)

5
  • Is this string your whole json? Valid json must start with { or [. Commented Mar 9, 2018 at 13:30
  • This string is just part of json i want to read. Whole json is big file. Commented Mar 9, 2018 at 13:31
  • Post Json with the proper format then we can help you extract the content Commented Mar 9, 2018 at 13:32
  • Hmm did you try convert to JSONObject instead of JSONArray? Commented Mar 9, 2018 at 13:32
  • Yes, I tried like JSONObject connectTo = (JSONObject) config.get("connectTo"); but throwing casting exception. Commented Mar 9, 2018 at 13:35

3 Answers 3

3

I just assume config is a JSONObject.

JSONArray connectTo = config.getJSONArray("connectTo");

config.get() returns an Object while config.getJSONArray() returns a JSONArray.

Sign up to request clarification or add additional context in comments.

9 Comments

but same i said above for config.getJSONArray, it is showing cannot resolve method getJSONArray
What is the output of System.out.println(config.toString());?
{"cluster":{"simlURL":"wss:\/\/localhost:8200\/opt\/siml","clusterName":"cluster0","enabled":"true"},"tempLogins":[{"password":"root","username":"root"}],"autoPromoteNewConnectionsFromPurgatory":true,"turnOnExtraSIMLWebSocket":"false","SIMLWebPort":8200,"persistFolder":"clusterconfig\/concentratorAPersist","localWebProxyServer":"localhost","SIMLProxyPort":8100,"_sslDirectory":"clusterconfig\/certD","connectTo":[],"sslCrtFile":"clusterconfig\/certDirA\/siml.crt","localWebProxyPort":8080,"sslKeyFile":"clusterconfig\/certDirA\/siml.key","simlFieldWatchUrl":"fieldwatchholder.jsp"}
I ma sorry its messy but it is Json string
It looks like your "connectTo" IS actually empty. So there's nothing wrong with the code. And this JSON is completely different from the one in the question.
|
1

Try this:

JSONArray connectTo =config.getJSONArray("connectTo");

                    System.out.println("Connect to : " + connectTo);
                    for (int i=0;i<connectTo.length();i++) 
    {
                        JSONObject connect = connectTo.getJSONObject(i);
                        String url = connect.get("url");
                        System.out.println(url);
                    }

5 Comments

for config.getJSONArray, it is showing cannot resolve method getJSONArray.
Where have you defined your config variable?
JSONObject config = (JSONObject) new JSONParser().parse(new FileReader(file)); and file is path of json file.
@p05p JSONObject config = new JSONObject(new JSONParser().parse(new FileReader(file));
still no luck :(
0

I got it working like this, not sure if this helps:

var json = {
  "connectTo": [{
    "url": "wss://localhost/opt/siml"
  }]
};

for (var o = 0; o < json.connectTo.length; o++) {
  console.log(json.connectTo[o].url);
}

1 Comment

I am sorry but for this i have to modify my json string. ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.