【问题标题】:Getting String Value from Json Object Android从 Android 的 Json 对象获取字符串值
【发布时间】:2012-01-04 06:10:30
【问题描述】:

我是 Android 的初学者。在我的项目中,我从 HTTP 响应中获取了以下 json。

[{"Date":"2012-1-4T00:00:00",
"keywords":null,
"NeededString":"this is the sample string I am needed for my project",
"others":"not needed"}]

我想从上面的 json 中获取“NeededString”。如何获得?

【问题讨论】:

标签: android json parsing


【解决方案1】:

这可能会对你有所帮助。

Java:

JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("NeededString");

科特林:

val jsonArray = JSONArray(result)
val jsonObject: JSONObject = jsonArray.getJSONObject(0)
val date= jsonObject.get("NeededString")
  • getJSONObject(索引)。在上面的例子中,0 代表索引。

【讨论】:

  • 感谢您的快速回复。我得到了它。非常感谢。
  • 服务器端发送响应的php代码是什么?
  • 这是我的 json 格式,我得到了下面给出的 json 字符串。但我想得到特定的值,如何得到它? {“登录”:[{“状态”:“错误”}]}
  • 请帮助我理解,为什么在 getJSONobject 中将值零作为参数传递?谢谢
  • 我想通过调用其他人来获得需要的字符串。
【解决方案2】:

您只需要获取JSONArray 并使用循环在数组内迭代JSONObject,尽管在您的情况下它只有一个JSONObject,但您可能有更多。

JSONArray mArray;
        try {
            mArray = new JSONArray(responseString);
             for (int i = 0; i < mArray.length(); i++) {
                    JSONObject mJsonObject = mArray.getJSONObject(i);
                    Log.d("OutPut", mJsonObject.getString("NeededString"));
                }
        } catch (JSONException e) {
            e.printStackTrace();
        }

【讨论】:

  • 感谢您的快速回复。
  • 服务器端发送响应的 php 代码是什么? (使用该键“NeededString”)
【解决方案3】:

在您的项目中包含org.json.jsonobject

然后你可以这样做:

JSONObject jresponse = new JSONObject(responseString);
responseString = jresponse.getString("NeededString");

假设,responseString 持有您收到的回复。

如果您需要知道如何将收到的响应转换为字符串,请按以下步骤操作:

ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();

【讨论】:

    【解决方案4】:

    您可以使用getString

    String name = jsonObject.getString("name");
    // it will throws exception if the key you specify doesn't exist
    

    optString

    String name = jsonObject.optString("name");
    // it will returns the empty string ("") if the key you specify doesn't exist
    

    【讨论】:

      【解决方案5】:

      如果你可以使用JSONObject 库,你可以

          JSONArray ja = new JSONArray("[{\"Date\":\"2012-1-4T00:00:00\",\"keywords\":null,\"NeededString\":\"this is the sample string I am needed for my project\",\"others\":\"not needed\"}]");
          String result = ja.getJSONObject(0).getString("NeededString");
      

      【讨论】:

        【解决方案6】:

        这是从 ResponseEntity 获取元素的代码

        try {
                    final ResponseEntity<String> responseEntity = restTemplate.exchange(API_URL, HttpMethod.POST, entity, String.class);
                    log.info("responseEntity"+responseEntity);
                    final JSONObject jsonObject ; 
                    if (responseEntity.getStatusCode() == HttpStatus.CREATED) {
                        try {
                            jsonObject = new JSONObject(responseEntity.getBody());
                            final String strName = jsonObject.getString("name");
                            log.info("name:"+strName);
                        } catch (JSONException e) {
                            throw new RuntimeException("JSONException occurred");
                        }
                    }
                }catch (HttpStatusCodeException exception) {
                    int statusCode = exception.getStatusCode().value();
                    log.info("statusCode:"+statusCode);
                }
        

        【讨论】:

          【解决方案7】:

          这是我为我使用的解决方案 适用于从字符串中获取 JSON

          protected String getJSONFromString(String stringJSONArray) throws JSONException {
                  return new StringBuffer(
                         new JSONArray(stringJSONArray).getJSONObject(0).getString("cartype"))
                             .append(" ")
                             .append(
                         new JSONArray(employeeID).getJSONObject(0).getString("model"))
                        .toString();
              }
          

          【讨论】:

            【解决方案8】:

            我觉得对你有帮助

                            JSONArray jre = objJson.getJSONArray("Result");
            
                            for (int j = 0; j < jre.length(); j++) {
                                JSONObject jobject = jre.getJSONObject(j);
            
                                String  date = jobject.getString("Date");
                                String  keywords=jobject.getString("keywords");
                                String  needed=jobject.getString("NeededString");
            
                            }
            

            【讨论】:

              【解决方案9】:

              请在下面查看我的答案,灵感来自上面的答案,但更详细...

              // Get The Json Response (With Try Catch)
              try {
              
                  String s = null;
              
                  if (response.body() != null) {
              
                      s = response.body().string();
              
                      // Convert Response Into Json Object (With Try Catch)
                      JSONObject json = null;
              
                      try {
                          json = new JSONObject(s);
              
                          // Extract The User Id From Json Object (With Try Catch)
                          String stringToExtract = null;
              
                          try {
                              stringToExtract = json.getString("NeededString");
              
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
              
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
              
                  }
              
              } catch (IOException e) {
                  e.printStackTrace();
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2023-03-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-07-30
                • 1970-01-01
                相关资源
                最近更新 更多