2017-03-05 106 views
1

我有一個json字符串字符串,我想從中獲取值。它是密鑰對形式,所以我需要每個密鑰的值。如何從JSON中獲取值字符串

我試圖得到的值,但我沒有得到它。

JSON字符串是這樣的:

{ 
    "document": { 
     "xmlns:xsi": "http:\/\/www.w3.org\/2001\/XMLSchema-instance", 
     "xsi:schemaLocation": "http:\/\/ocrsdk.com\/schema\/recognizeard-1.0.xsd http:\/\/ocrsdk.com\/schema\/recognized-1.0.xsd", 
     "xmlns": "http:\/\/ocrsdk.com\/schema\/recognize-1.0.xsd", 
     "businessCard": { 
      "imageRotation": "noRotation", 
      "field": 

       [{ 
       "type": "Name", 
       "value": "Rafcev B. Agnrwal" 
      }, { 
       "type": "Company", 
       "value": "VJ>" 
      }, { 
       "type": "Text", 
       "value": "Dr. Rafcev B. Agnrwal\nMOB 0324%\nun\n) AOM*. founts. sso\nVJ>\nT" 
      }] 
     } 
    } 
} 

我想獲得 「姓名」, 「地址」, 「公司」 等的值

我試圖讓這樣的:

JSONArray array; 
if(mIntent.getStringExtra("jsonString") != null) { 
      Log.d("json", mIntent.getStringExtra("jsonString")); 

     try { 
      JSONObject object = new JSONObject(mIntent.getStringExtra("jsonString")); 

      array = object.getJSONArray("field"); 

      for (int i = 0; i < array.length(); i++) { 
       JSONObject subObject1 = array.getJSONObject(i); 

       edt_FirstName.setText(object.getString("Name")); 
      } 

     } catch (JSONException e) { 

} 

任何人都可以幫我嗎?謝謝。

編輯:

我試圖檢查這樣的價值觀:

   for (int i = 0; i < array.length(); i++) { 
        JSONObject subObject1 = array.getJSONObject(0); 

        String type = subObject1.getString("type"); 
        String value = subObject1.getString("value"); 
        if (type.equals("Name")) { 
         edt_FirstName.setText(value); 
        } 
        if(type.equals("Address")) 
        { 
         edt_address.setText(value); 
        } 
        if(type.equals("Company")) 
        { 
         edt_company.setText(value); 
        } 
        if(type.equals("Mobile")) 
        { 
         edt_Mobile.setText(value); 
        } 


       } 

我想從外地陣列中的所有值,並設置爲文本視圖,但我正在逐漸只有名稱值而不是其他值。

另外我可以像移動設備那樣獲得兩次移動字段,我可以獲得兩次,所以我想將兩個移動設備值合併並顯示出來。

回答

2

所以你的JSON對象是如下:

{ 
    "document": { 
     "xmlns:xsi": "http:\/\/www.w3.org\/2001\/XMLSchema-instance", 
     "xsi:schemaLocation": "http:\/\/ocrsdk.com\/schema\/recognizeard-1.0.xsd http:\/\/ocrsdk.com\/schema\/recognized-1.0.xsd", 
     "xmlns": "http:\/\/ocrsdk.com\/schema\/recognize-1.0.xsd", 
     "businessCard": { 
      "imageRotation": "noRotation", 
      "field": 

       [{ 
       "type": "Name", 
       "value": "Rafcev B. Agnrwal" 
      }, { 
       "type": "Company", 
       "value": "VJ>" 
      }, { 
       "type": "Text", 
       "value": "Dr. Rafcev B. Agnrwal\nMOB 0324%\nun\n) AOM*. founts. sso\nVJ>\nT" 
      }] 
     } 
    } 
} 

你首先需要獲得「文件」爲JSONObject,然後得到「的businesscard」爲JSONObject,然後就可以得到「場」爲JSONArray

if(mIntent.getStringExtra("jsonString") != null) { 
    Log.d("json", mIntent.getStringExtra("jsonString")); 

    try { 
     JSONObject object = new JSONObject(mIntent.getStringExtra("jsonString")); 

     JSONArray array = object.getJSONObject("document").getJSONObject("businessCard").getJSONArray("field"); 

     for (int i = 0; i < array.length(); i++) { 
      JSONObject subObject = array.getJSONObject(i); 

      String type = subObject.getString("type"); 
      String value = subObject.getString("value"); 
      if (type.equals("Name")) { 
        String prevValue = edt_FirstName.getText(); 
        edt_FirstName.setText((TextUtils.isEmpty(prevValue) ? "" : prevValue + ",") + value); 
      } 
     } 

    } catch (JSONException e) { } 
} 
+0

以及如何從字段數組中獲取值? – Sid

+0

@Sid更新的詳細信息 – manman

+0

你能檢查編輯的問題嗎? – Sid

1

試試這個代碼,希望它能幫到你。

try 
{ 
    JSONObject jsonObject = new JSONObject(); 
    JSONObject documentObject = jsonObject.getJSONObject("document"); 
    JSONObject businessCardObject = documentObject.getJSONObject("businessCard"); 

    String imgRotation = businessCardObject.getString("imageRotation"); 
    JSONArray array = businessCardObject.getJSONArray("field"); 

    for (int i = 0; i < array.length() ; i++) 
    { 
     JSONObject arrObj = array.getJSONObject(i); 

     String type = arrObj.getString("type"); 
     String value = arrObj.getString("value"); 

     Log.e(TAG, "type=="+type); 
     Log.e(TAG, "value=="+value); 
    } 
} 
catch (Exception ex) 
{ 
    ex.printStackTrace(); 
}