Java 将字符串转换为JsonArray

Java 将字符串转换为JsonArray,java,android,json,Java,Android,Json,如何正确地将该字符串转换为jsonArray { "myArray": [ { "id": 1, "att1": 14.2, "att2": false }, { "id": 2, "att1": 13.2, "att2": false }, { "id": 3, "att1": 13, "att2": false } ]} JSONObject jo

如何正确地将该字符串转换为jsonArray

{
    "myArray": [
    {   "id": 1,
        "att1": 14.2,
        "att2": false },
    {   "id": 2,
        "att1": 13.2,
        "att2": false },
    {   "id": 3,
        "att1": 13,
        "att2": false }
  ]}
JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list
An
JSONArray jArray=新的JSONArray(上面的字符串)导致
jArray.length=1

这是我第一次接触json:)

看看JQuery,特别是Parse函数。这将直接将Json数组解析为对象。

尝试以下操作:

JSONObject jObject = new JSONObject(STRING_FROM_ABOVE);
JSONArray jArray = jObject.getJSONArray("myArray");
JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list
“上面的字符串”不是一个Json数组,它是一个Json对象,包含一个属性(myArray),它是一个Json数组;)

JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list
然后,您可以执行以下操作:

for (int i = 0; i < jArray.length(); i++) {
        JSONObject jObj = jArray.getJSONObject(i);
        System.out.println(i + " id : " + jObj.getInt("id"));
        System.out.println(i + " att1 : " + jObj.getDouble("att1"));
        System.out.println(i + " att2 : " + jObj.getBoolean("att2"));
}
JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list
for(int i=0;i
解析时,向对象和ArrayList添加值,或者以您希望的方式添加值。祝你好运

JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list
JSONObject jo=新的JSONObject(上面的字符串);
JSONArray rootArray=jo.getJSONArray(“myArray”);
int rootArrayLength=rootArray.length();

对于(int i=0;i,为了在这里混合使用另一种方法,我建议您看一看.Gson是一个库,它使Java对象的序列化和反序列化变得简单。例如,使用字符串,您可以执行以下操作:

JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list
// Declare these somewhere that is on the classpath
public class ArrayItem{
    public int id;
    public double att1;
    public boolean att2;
}

public class Container{
    public List<ArrayItem> myArray;
}

// In your code
Gson gson = new Gson();

Container container = gson.fromJson(json, Container.class);

for(ArrayItem item : container.myArray){
    System.out.println(item.id);  // 1, 2, 3
    System.out.println(item.att1);  // 14.2, 13.2, 13.0
    System.out.println(item.att2);  // false, false, false
}

使用Gson之类的工具的主要好处是,您现在可以在默认情况下使用所有Java的类型检查,而不必自己管理属性名称和类型。它还允许您执行一些奇特的操作,例如复制类型层次结构,从而使管理大量json消息变得简单。它与Andr非常配合oid,jar本身很小,不需要任何额外的依赖项。

字符串是一个JSON对象,而不是数组(注意最外面的大括号)。非常感谢……我真的应该更仔细地研究JSON及其语法:)没问题,但别忘了接受答案:)所以你不能说jArray[i];哈?我搞不懂你…我有相同的prb…上面的字符串是什么?如果myArray是上面的字符串,那么为什么要在JSONArray中写“myArray”…自动更正!不管怎样,它的语音是正确的。
JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list