Parse JSON Android convert jsonObject

偶尔善良 提交于 2019-12-12 03:49:51

问题


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();
    }

回答1:


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);
}



回答2:


JSONObject obj = new JSONObject(test2);
JSONObject data=obj.getJSONobject("data");
JSONObject ob1=obj.getJSONobject("1");
String pageName = ob1.getString("Name");



回答3:


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



来源:https://stackoverflow.com/questions/40103987/parse-json-android-convert-jsonobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!