2017-03-05 109 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(); 
}