简体   繁体   中英

Parse JSON Android convert jsonObject

I have a problem with JSON I get a json since https://proxyepn-test.epnbn.net/wsapi/epn

But when I want to display a single data eg "name". The console displays:

Log

org.json.JSONException: No value for Name
org.json.JSONException: Value status at 0 of the type java.lang.String can not be converted to JSONObject

Can you help me ? Thanks.

here is my code :

String test2 = test.execute(restURL).get().toString();
        Log.i("result",test2);
JSONObject obj = new JSONObject(test2);
        String data = obj.getString("data");
        Log.i("testjson",data);
        String pageName = obj.getJSONObject("data").getString("Name");
        Log.i("testjsondata",pageName);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
JSONObject obj = new JSONObject(test2);
JSONObject data=obj.getJSONobject("data");
JSONObject ob1=obj.getJSONobject("1");
String pageName = ob1.getString("Name");

Try below:

JSONObject obj = new JSONObject(test2);
JSONObject data = obj.getJSONObject("data");
Iterator<String> iterator = data.keys();
while(iterator.hasNext()){
        String key = iterator.next();
        String Name = data.getString(key);
}

You have to parse your next level of JSONObject (labeled as "1","2","3".. from response).

It seems like issue in Json response structure you shared. why cann't it be array inside "data"?

Then you can easily read data as JSONArray with those objects as ("1","2","3"..) array item.

Else

Android JSON parsing of multiple JSONObjects inside JSONObject

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