Java 在android中解析没有名称的json数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18416585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:27:00  来源:igfitidea点击:

Parsing json array with no name in android

javaandroidjsonparsing

提问by Maha Mahmoud

I have a Json Array as string with no name and I want to parse it how can i do it in android ?

我有一个 Json Array 作为没有名称的字符串,我想解析它我该如何在 android 中做到这一点?

My JsonArray is :

我的 JsonArray 是:

  [
  { 
    "categoryId":1,
    "Title":"Rock",
    "songs":null
  },
    { 
    "categoryId":2,
    "Title":"Jaz",
    "songs":null
    }
  ]

采纳答案by Hariharan

Try this..

尝试这个..

JSONArray jsonarray = new JSONArray(json);

for (int i = 0; i < jsonarray.length(); i++) {

       JSONObject jsonobj = jsonarray.getJSONObject(i);

      System.out.println("categoryId : " + i + " = " + jsonobj.getString("categoryId"));
       System.out.println("Title : " + i + " = " + jsonobj.getString("Title"));
       System.out.println("songs : " + i + " = " + jsonobj.getString("songs"));
}

回答by Henry

You can use:

您可以使用:

JSONArray a = new JSONArray(myJsonString);

回答by Biraj Zalavadia

Do like this

这样做

JSONArray jArr = new JSONArray(jsonString);

for (int i = 0; i < jArr.length(); i++) {
    Syste.out.println("Object : " + i + " = " + jArr.getJSONObject(i));
}

回答by shiju B

Try this,

尝试这个,

JSONArray array1=new JSONArray(json);
for(int i=0;i<array1.length;i++)
{
JSONObject obj1 = array1.getJSONObject(i);
    String categoryId = obj1.getString("categoryId");
    String Title = obj1.getString("Title");
    String songs = obj1.getString("songs");
}

回答by Kavin

Try this:

尝试这个:

JSONArray arr = new JSONArray(jsonString);

for (int i = 0; i < arr.length(); i++) {
    JSONObject obj = arr.getJSONObject(i);

     String title = obj.getString("Title");
}