简体   繁体   中英

Not able to convert String to JSONObject

I am getting an inputstream and converting it to String and then to JSONObject

Below is snippet for converting inputstream to String and then JSONObject.

But after converting to json object(line 6) I am getting only the first object instead of all the objects

Can you please let me know why I am getting only one object instead of all the n objects

InputStream in = new BufferedInputStream(conn.getInputStream());
String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
int i =result.indexOf("{");
String forResult=result.substring(i);
System.out.println(forResult); // Result 1
JSONObject jsonObject = new JSONObject(forResult); // Line 6
System.out.println(jsonObject); // Result 2

After converting it to String it look like this

Result -1

{  
   "test_expr":"",
   "val_expr":"someVale",
   "val_advanced":true,
   "machine_val":null
}, {...// n times}

Result-2 -only first object

{  
     "test_expr":"",
       "val_expr":"someVale",
       "val_advanced":true,
       "machine_val":null
    } 

Thanks and please bear my ignorance as I am completly new in java

Because you json is not valid .There is a comma between JSONObject .

Change to this .

{
    {
    "test_expr":"",
    "val_expr":"someVale",
    "val_advanced":true,
    "machine_val":null
    },
    {
    "test_expr":"",
    "val_expr":"someVale",
    "val_advanced":true,
    "machine_val":null
    }
    ...
}

or

 [
    {
    "test_expr":"",
    "val_expr":"someVale",
    "val_advanced":true,
    "machine_val":null
    },
    {
    "test_expr":"",
    "val_expr":"someVale",
    "val_advanced":true,
    "machine_val":null
    }
    ...
]

The source of JSONObject

/**
 * Creates a new {@code JSONObject} with name/value mappings from the JSON
 * string.
 *
 * @param json a JSON-encoded string containing an object.
 * @throws JSONException if the parse fails or doesn't yield a {@code
 *     JSONObject}.
 */
public JSONObject(String json) throws JSONException {
    this(new JSONTokener(json));
}

So a JSON-encoded string containing an object(like {}).

// make sure that you result contain {}
result = "{your data here}"
JSONObject json_data = new JSONObject(result);

And if you use JSONArray ,you should contain [] in your JSON

result = "[json data]";
JSONArray jsonArray = new JSONArray(result);

Well, concatenation of multiple jsons is not a valid json. Your parsing library should have rejected such an input, but it seems that it just stopped at the end of the valid object.

You can wrap the list it into an array: [{...},{...},{...}]. Then the parser will be able to correctly interpret it as an array.

I guess you are getting JSONArray object instead of JSONObject. Why do you need to get sub string of the result? Json array can start with [. See the difference between JSONObject and JSONArray. Difference between JSONObject and JSONArray

Thanks to Sotirios Delimanolis.

I am able to resolve the problem by using JSONParser

InputStream in = new BufferedInputStream(conn.getInputStream());
        String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
        JSONParser parser = new JSONParser();
        org.json.simple.JSONArray modelArrary =(org.json.simple.JSONArray) parser.parse(result) ;
        System.out.println(modelArrary);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM