Java 如何循环使用这个示例JSON数组来获得问题名称、问题精度和专门化名称

Java 如何循环使用这个示例JSON数组来获得问题名称、问题精度和专门化名称,java,json,Java,Json,我看过文件,但没有一份能给我所需要的直接答案。我正在尝试循环使用java中apimedic返回的诊断数组。下面是它的样子 [ { Issue: { ID: 11, Name: "Flu", Accuracy: 90, Icd: "J10;J11", IcdName: "Influenza due to other identified influenza virus;Influenza,

我看过文件,但没有一份能给我所需要的直接答案。我正在尝试循环使用java中apimedic返回的诊断数组。下面是它的样子

[

  {
    Issue: 
    {
      ID: 11,
      Name: "Flu",
      Accuracy: 90,
      Icd: "J10;J11",
      IcdName: "Influenza due to other identified influenza 
      virus;Influenza, 
      virus not identified",
      ProfName: "Influenza",
      Ranking: 1
    },
    Specialisation: 
    [
      {
       ID: 15,
       Name: "General practice",
       SpecialistID: 0
      },
      {
       ID: 19,
       Name: "Internal medicine",
       SpecialistID: 0
      }
    ]
  },
   {
     Issue: 
     {
       ID: 281,
       Name: "Food poisoning",
       Accuracy: 87.5,
       Icd: "A05;A02;A03;A04",
       IcdName: "Other bacterial foodborne intoxications, not elsewhere 
       classified;Other salmonella infections;Shigellosis; 
       intestinal infections",
       ProfName: "Foodborne illness",
       Ranking: 2
     },
      Specialisation: 
     [
       {
         ID: 15,
         Name: "General practice",
         SpecialistID: 0
       },
      {
       ID: 19,
       Name: "Internal medicine",
       SpecialistID: 0
      }
     ]
    },
     {
      Issue: 
     {
        ID: 376,
        Name: "Scarlet fever",
        Accuracy: 13.125,
        Icd: "A38",
        IcdName: "Scarlet fever",
        ProfName: "Scarlatina",
        Ranking: 3
     },
      Specialisation: 
    [
      {
        ID: 15,
        Name: "General practice",
        SpecialistID: 0
     },
     {
        ID: 23,
        Name: "Infectiology",
        SpecialistID: 0
     },
     {
        ID: 19,
        Name: "Internal medicine",
        SpecialistID: 0
      }
    ]
  }
]
我正在尝试获取问题名称、问题准确性和专业名称的值。我已经能够做其他事情了,但这真的给了我一段艰难的时间。任何帮助都将不胜感激。多谢各位

以下是我尝试过的:

try {

                JSONArray jsonArray = new JSONArray(result);
                    //get the conditions
                    for(int i=0; i<jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        String id = jsonObject.getString("Name");

                        Log.i("Issue: ", id);

                                //get the specializations inside every condition
                                JSONArray internalArray = new JSONArray(id);
                                for(int a=0; a<internalArray.length(); a++) {
                                    JSONObject jsonObjectInternal = internalArray.getJSONObject(a);
                                    String name = jsonObjectInternal.getString("Name");

                                    //display the specializations
                                    Log.i("Specialization", name);

                                    diagnosis.add(new DiagnosisModel("Headache", "009" , "General"));
                                }
                       // String name = jsonObject.getString("Name");

                        //possibleConditions.add(id);
                        Log.i("Line: ", " : " + i);
                }
我尝试使用的Api是ApiMedic。如果有帮助的话,请看一下。
这都是关于结构的。伪JSON以
[
开头,因此它是一个数组。 该数组的每个元素都以
{
开头,所以它们都是对象

此数组中的每个对象都有两个属性名称
Issue
specialization

Issue
的值以
{
开头,因此它是一个对象。它有一个名为
Name
的属性,类型为String。因此您可以使用(这是伪代码,但您应该能够将其转换为代码)获取该属性值

准确性是issue对象的另一个属性,类型为number,因此您可以通过基本相同的方式获得它:

rootArray.getObjectAtIndex[i].getObjectNamed("Issue").getNumberNamed("Accuracy")
现在,Specialization的值以
[
开头,所以它是一个数组。它包含的每个元素都以
{
开头,所以它们是对象。每个对象都有一个字符串类型的属性
名称
。因此使用

rootArray.getObjectAtIndex[i].getObjectNamed("Specialisation").getObjectAtIndex(j).getStringNamed("Name")

多亏了@JBnizet。我终于在你的建议下找到了答案。下面是真正的代码,完全符合我的意思

     try {
           //returns result of async task with api url
            JSONArray jsonArray = new JSONArray(result);
           //For getting the Name and Accuracy of the issue object
            for(int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                jsonArray.getJSONObject(i).getJSONObject("Issue").toString();

                Log.i("IssueName: ",jsonArray.getJSONObject(i).getJSONObject("Issue").getString("Name"));
                Log.i("IssueAccuracy", jsonArray.getJSONObject(i).getJSONObject("Issue").getString("Accuracy"));

                //for getting the specialisation of the issue(inner specialisation array attributes)
                int arraylength = jsonArray.getJSONObject(i).getJSONArray("Specialisation").length();
                for (int j = 0; j < arraylength; j++) {
                    Log.i("Specialisation: ", jsonArray.getJSONObject(i).getJSONArray("Specialisation").getJSONObject(j).getString("Name"));
                }
            }catch (JSONException e) {
                Log.i("MESSAGE 3: ", e.toString());
试试看{
//返回带有api url的异步任务的结果
JSONArray JSONArray=新JSONArray(结果);
//用于获取问题对象的名称和准确性

对于(int i=0;它不是JSON。即使它是,它也没有缩进,这使得理解它的结构非常困难。如果你不能理解它的结构,编写代码从这个结构中提取信息是不可能的。发布有效的缩进JSON,发布你尝试过的代码,并告诉你使用的几十个JSON解析器中的哪一个。这看起来确实像JSON,但正如@JBNizet所提到的,它不是。键总是双引号(比如
“key”:“value”
)。也就是说,JSON很容易被像Gson的Jackson这样的JSON反序列化程序反序列化。这方面的任何帮助都会非常有用。我已经为此搜索了好几天。从那时起就一直被卡住了。@felix但是,当有人告诉你缩进JSON,显示你尝试了什么,并指定你正在使用的JSON库时,你可以vide zero answer.@JBNizet我很抱歉响应太晚,有一段时间没有上网。我已经尝试更好地缩进JSON,并添加了我尝试过的代码。我还声明了项目中导入的JSON库。我也是android/java应用程序开发的新手。非常感谢。非常感谢@JBNizet。这篇文章ked很好。哇。我希望我能投票。stackoverflow需要允许人们在没有严格规则的情况下为有用的答案投票。@felix你至少应该将答案标记为已接受(通过单击答案左侧的灰色勾号)。投票很快就会到来。就这么做了。谢谢提示!
rootArray.getObjectAtIndex[i].getObjectNamed("Specialisation").getObjectAtIndex(j).getStringNamed("Name")
     try {
           //returns result of async task with api url
            JSONArray jsonArray = new JSONArray(result);
           //For getting the Name and Accuracy of the issue object
            for(int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                jsonArray.getJSONObject(i).getJSONObject("Issue").toString();

                Log.i("IssueName: ",jsonArray.getJSONObject(i).getJSONObject("Issue").getString("Name"));
                Log.i("IssueAccuracy", jsonArray.getJSONObject(i).getJSONObject("Issue").getString("Accuracy"));

                //for getting the specialisation of the issue(inner specialisation array attributes)
                int arraylength = jsonArray.getJSONObject(i).getJSONArray("Specialisation").length();
                for (int j = 0; j < arraylength; j++) {
                    Log.i("Specialisation: ", jsonArray.getJSONObject(i).getJSONArray("Specialisation").getJSONObject(j).getString("Name"));
                }
            }catch (JSONException e) {
                Log.i("MESSAGE 3: ", e.toString());