简体   繁体   中英

how to get JSON response covered by XML tag?

please tell me how to get JSON response covered by XML tag?

<string>[
  {
    "value": "America",
    "id": "1898"
  },
  {
    "value": "Afghanistan",
    "id": "546"
  },
  {
    "value": "China",
    "id": "119"
  },
  {
    "value": "Bhutan",
    "id": "1884"
  }
]<string>

hi please find the solution as follows, use below code

String jsonData = null;
    XmlPullParserFactory factory;
    try {
        factory = XmlPullParserFactory.newInstance();

        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        AssetManager manager = getAssets();
        InputStream stream;

        stream = manager.open("data.json");

         xpp.setInput(new StringReader (getStringFromInputStream(stream)));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_DOCUMENT) {
                System.out.println("Start document");
            } else if (eventType == XmlPullParser.END_DOCUMENT) {
                System.out.println("End document");
            } else if (eventType == XmlPullParser.START_TAG) {
                System.out.println("Start tag " + xpp.getName());
            } else if (eventType == XmlPullParser.END_TAG) {
                System.out.println("End tag " + xpp.getName());
            } else if (eventType == XmlPullParser.TEXT) {
                System.out.println("Text " + xpp.getText());
                jsonData= xpp.getText();
            }
            eventType = xpp.next();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //parsing json
    try {
        JSONArray jarray=new JSONArray(jsonData);
        for(int i=0;i<jarray.length();i++){
            JSONObject jobject=jarray.getJSONObject(i);
            String value=jobject.getString("value");
            String id=jobject.getString("id");
            System.out.println("value " + value);
            System.out.println("id " + id);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

the getStringFromInputStream method is as follows

private static String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();

}

for solving yours problem i just put yours given data at asset folder with name data.jason later you can get this data from web services.

<string>[
  {
    "value": "America",
    "id": "1898"
  },
  {
    "value": "Afghanistan",
    "id": "546"
  },
  {
    "value": "China",
    "id": "119"
  },
  {
    "value": "Bhutan",
    "id": "1884"
  }
]<string>

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