-1

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

brk
  • 48,835
  • 10
  • 56
  • 78
  • 1
    `JSONObject` won't parse further than the enclosing `}`. Was the content from the stream a JSON array instead? Why are you substringing? – Sotirios Delimanolis Oct 29 '17 at 05:56
  • @SotiriosDelimanolis it look like this this [{...},{...}...] after converting it using `org.apache.commons.io.IOUtils.toString(in, "UTF-8");` – brk Oct 29 '17 at 06:01
  • Ok, so it's a JSON array. Use `JSONArray` to parse it. – Sotirios Delimanolis Oct 29 '17 at 06:01
  • Possible duplicate of [JSONException: Value of type java.lang.String cannot be converted to JSONObject](https://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject) – KeLiuyue Oct 29 '17 at 06:04

4 Answers4

1

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);
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • If their JSON was not valid, then `JSONObject` would fail to parse it. Also your first snippet is in fact invalid. Please validate your answer before posting it. – Sotirios Delimanolis Oct 29 '17 at 06:00
  • I also notice that the last part of your answer is a copy of another recent answer of yours. If two questions have the same answer, they should be considered duplicates, and you should be voting to close them as such. You have access to that privilege, use it. – Sotirios Delimanolis Oct 29 '17 at 06:01
  • And i use privilege .There is a comma between `JSONObject` .Isn't OK ?@SotiriosDelimanolis – KeLiuyue Oct 29 '17 at 06:06
0

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.

Mateusz Stefek
  • 3,478
  • 2
  • 23
  • 28
0

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

Hai Quach
  • 7
  • 5
0

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);
brk
  • 48,835
  • 10
  • 56
  • 78