Search code examples
orientdb

JSON error on OrientDB Results


I am getting an error attempting to parse the JSON results. My http string looks like this :

String query_str = "http://192.168.1.67:2480/query/GratefulDeadConcerts/sql/select%20name%20from%20V%20where%20name='BERTHA'";

which should read : select name from V where name='BERTHA'

this returns query result:{"result":[{"@type":"d","@rid":"#-2:0","@version":0,"name":"BERTHA"}]}

My parse code looks like this:

try {
Logging.log ("parseContent", "Entered in parseContent");
   JSONObject jsonData = new JSONObject(contentStr);
   JSONArray itemsArray = jsonData.getJSONArray(DEAD_result);
   Logging.log("parseContent", "After object Creation  ");
   for(int i=0; i<itemsArray.length(); i++) {
        Logging.log("parseContent", "Before name parse ");
        String name = jsonData.getString(DEAD_SONG_NAME);
        String song_type  = jsonData.getString(DEAD_SONG_TYPE);
        String type = jsonData.getString(DEAD_TYPE);
        String performances =jsonData.getString(DEAD_PERFORMANCES);

        }

I get the error:

org.json.JSONException: No value for name

at org.json.JSONObject.get(JSONObject.java:355) at org.json.JSONObject.getString(JSONObject.java:515)

I thinks this means the JSON object can not find a 'name' field in the JSON string. You can see the name field in the results. There are some additional fields also.

OrientDB 2.1.9

Thanks


Solution

  • Try with this code

    String contentStr="{'result':[{'@type':'d','@rid':'#-2:0','@version':0,'name':'BERTHA'}]}";
    try {
        JSONObject jsonData = new JSONObject(contentStr);
        JSONArray itemsArray = jsonData.getJSONArray("result");
        for(int i=0; i<itemsArray.length(); i++) {
            JSONObject jsonObject = itemsArray.getJSONObject(i);
            String  name= jsonObject.getString("name");
        }
    }