简体   繁体   English

如何在Android中读取此JSON? 我对jsonobject和json字符串感到困惑

[英]How to read this json in android? I am confused with jsonobject and json string

I am confused as to what a JSON Object is and what a JSON String is. 我对什么是JSON对象和什么是JSON字符串感到困惑。 Which part is a JSON Object, and which is a JSON String? 哪一部分是JSON对象,哪一部分是JSON字符串?

JSON example 1: JSON范例1:

{ 
    "abc":"v1",
    "def":"v2"
}

JSON example 2: JSON示例2:

{
    "res":"false",
    "error":{
        "code":101
    }
}

Data is represented in name/value pairs. 数据以名称/值对表示。

"abc":"v1"

Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). 圆括号支撑对象,每个名称后跟':'(冒号),名称/值对之间以,(逗号)分隔。

{ 
"abc":"v1",
"def":"v2"
}

Code Example: 代码示例:

JSONObject obj = new JSONObject(jsonStr);
String abc = obj.get("abc");

Square brackets hold arrays and values are separated by ,(comma). 方括号包含数组,并且值之间以,(逗号)分隔。

{
   "books": [

      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt",
      },

      {
         "id":"07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy",
      }

   ]
}

Code Example: 代码示例:

JSONArray arrBooks = new JSONArray("books");
for (int i = 0; i<=arrBooks.length(); i++){
      JSONObject objBook = arrBooks.getJSONObject(i);
      String id = c.getString("id");
}

Given by your first example: 由您的第一个示例给出:

try {
    JSONObject obj = new JSONObject(json);
    String abc = obj.get("abc");
    String def = obj.get("def");
} catch (Throwable t) {
    // Log something maybe?
}

Simply create a JSONObject with that string in the constructor. 只需在构造函数中使用该字符串创建一个JSONObject即可。

JSONObject obj = new JSONObject(your_string_goes_here);

Your JSON string is the entire visual representation that you see (encoded as a string): JSON字符串是您看到的整个视觉表示(编码为字符串):

{ 
    "abc":"v1",
    "def":"v2"
}

You can tell where a specific JSON Object starts and ends within your string, by looking for that opening brace { and the closing brace '}'. 您可以通过查找开头大括号{和结尾大括号'}'来判断特定JSON对象在字符串中的开始和结束位置。

In your examples, this is a JSON Object: 在您的示例中,这是一个JSON对象:

{ 
    "abc":"v1",
    "def":"v2"
}

So is this: 这是这样的:

{
    "res":"false",
    "error": {
        "code":101
    }
}

And this: 和这个:

{
    "code":101
}

Use GSON for parsing & below are the model classes for json1 & json2 使用GSON进行解析&以下是json1和json2的模型类

public class Json1 {


    /**
     * abc : v1
     * def : v2
     */

    private String abc;
    private String def;

    public String getAbc() {
        return abc;
    }

    public void setAbc(String abc) {
        this.abc = abc;
    }

    public String getDef() {
        return def;
    }

    public void setDef(String def) {
        this.def = def;
    }
}

Json2 Json2

public class Json2 {

    /**
     * res : false
     * error : {"code":101}
     */

    private String res;
    /**
     * code : 101
     */

    private ErrorBean error;

    public String getRes() {
        return res;
    }

    public void setRes(String res) {
        this.res = res;
    }

    public ErrorBean getError() {
        return error;
    }

    public void setError(ErrorBean error) {
        this.error = error;
    }

    public static class ErrorBean {
        private int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}

I have used GsonFormatter plugin for creating model classes, Use Gson, It is super easy and you dont need to parse anything 我已经使用GsonFormatter插件创建了模型类,使用了Gson,它非常简单,您不需要解析任何内容

JSON comprises JSONObject & JSONArray. JSON包含JSONObject和JSONArray。 Json-1 is JSONObject while JSON -2 is also JSONObject which contains another JSONObject with a key "error" .JSON String is the string representation of JSONObject which you can get by JSONObject jsonObject = new JSONObject(); String jsonString = jsonObject.toString(); Json-1是JSONObject,而JSON -2也是JSONObject,其中包含另一个带有键"error" JSONObject。JSON字符串是JSONObject的字符串表示形式,您可以通过JSONObject jsonObject = new JSONObject(); String jsonString = jsonObject.toString();获得它JSONObject jsonObject = new JSONObject(); String jsonString = jsonObject.toString(); JSONObject jsonObject = new JSONObject(); String jsonString = jsonObject.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM