首页 > 解决方案 > 获取 JSON 对象的特定对象(XML 类型)

问题描述

<app:service xmlns:app="http://www.w3.org/2007/app"...>
 <app:workspace>
   <atom:title type="text">Data</atom:title>
  <app:collection . . .>
   <atom:title type="text">CourseContentSet</atom:title>
   <demo:member-title>CourseContent</demo:member-title>
  </app:collection>
  <app:collection . . .>
   <atom:title type="text">UnitContentSet</atom:title>
   <demo:member-title>UnitContent</demo:member-title>
  </app:collection>
 </app:workspace>
</app:service>

我怎样才能得到演示:会员头衔?

我试过这样做但没有成功......

         soapDatainJsonObject = XML.toJSONObject(soapmessageString);
        JSONObjectsongs=soapDatainJsonObject.getJSONObject("app:service");  
            JSONObject songs2 = songs.getJSONObject("app:workspace");
            String content = songs2.getString("app:collection")
            System.out.println(content);

如何访问 XML 文件的内部部分?通过 JSON。我想获取 CourseContent、UnitContent.. 等

标签: javajsonxmleclipse

解决方案


您需要解析每个深度。试试这个:

JSONObject soapDatainJsonObject = XML.toJSONObject(soapmessageString);
    JSONObject songs= soapDatainJsonObject.getJSONObject("app:service");
    JSONObject songs2 = songs.getJSONObject("app:workspace");
    JSONArray atomArray =  songs2.getJSONArray("app:collection");
    JSONObject atomTitle = atomArray.getJSONObject(0).getJSONObject("atom:title");
    String content = atomTitle.getString("content");
    System.out.println(content);

推荐阅读