2016-06-21 3 views
2

나는 없습니다. 확인란의 선택 (20), 내가 뭘했는지 사용자가 어떤 확인란을 선택하면, 그 이름은 배열에 저장됩니다 (예 : 코드에서 abc 배열). 해당 json을 저장하는 문자열 변수의 이름은 확인란의 이름과 같습니다. 예를 들어 체크 박스 "a"를 클릭하면 문자열 값 "a"가 배열에 저장되며 관련 json 값을 저장하는 "a"라는 문자열 변수가 있습니다. 필요한 것은 배열에 저장된 문자열 값을 InputStream = new ByteArrayInputStream (abc.get (i) .getBytes())로 전달하면 json의 inputStream을 구문 분석하는 데 사용해야합니다. 그러나 문자열 값 "a"가 문자열 변수 a와 같지 않기 때문에 NullPointerException을 제공합니다. 이 문제를 어떻게 해결할 수 있습니까? 나는 여기서 아이디어가 떨어졌다. 여기서하고 싶은 것을 성취 할 수있는 다른 방법이 있습니까?배열의 문자열 값을 문자열 변수로 사용하여 json-codenameone을 구문 분석합니다.

코드 : 선택된 체크 박스의 문자열 값은 JSON 파서 배열

String a = "[{\n" 
     + "\t\t\t\"title\": \"title1\",\n" 
     + "\t\t\t\"describ\": \"describ1\"\n" 
     + "}]"; 

String b = "[{\n" 
     + "\"title\": \"title2\",\n" 
     + "\"describ\": \"describ2\"\n" 
     + "}]"; 

String c = "[{\n" 
     + "\t\t\t\"title\": \"title3\",\n" 
     + "\t\t\t\"describ\": \"describ3\"\n" 
     + "}]"; 
//and all jsons required are there 

ArrayList<String> abc; 
@Override 
protected void beforeTestForApp(Form f) { 
    f.setTitle("abc"); 
    abc = new ArrayList<>(); 
    //I have stored "a" & "b" in the abc array here for simplicity, but it is dynamic, 
    //ie. if the user select checkbox c, c will be stored in abc array and so on 
    abc.add("a"); 
    abc.add("b"); 
    Button bb = new Button("go"); 
    bb.addActionListener((e) -> { 
     showForm("TestForAppResult", null); 
    }); 
    f.add(bb); 
} 

형태로 저장하고 값 표시 :

@Override 
protected void beforeTestForAppResult(Form f) { 
    f.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); 

    InputStream is; 
    for (int i = 0; i < abc.size(); i++) { 
     Label heading = new Label(abc.get(i)); 
     f.add(heading); 
     //this gives error since abc.get(i) gives string value, not string variable 
     is = new ByteArrayInputStream(abc.get(i).getBytes()); 
     showDetails(is, f); 
    } 
    //if i do this instead of for loop jst above, i can get the result but since what value'll be stored in an array is not known,it is not possible 
      //is = new ByteArrayInputStream(a.getBytes()); 
      //showDetails(is, f); 
      //is = new ByteArrayInputStream(b.getBytes()); 
      //showDetails(is, f); 
} 

private void showDetails(InputStream is, Form f) { 
    JSONParser p = new JSONParser(); 
    Hashtable<String, Object> test; 
    try { 
     test = p.parse(new InputStreamReader(is)); 
     Vector aVector = (Vector) test.get("root"); 
     for (int j = 0; j < aVector.size(); j++) { 
      Hashtable hm = (Hashtable) aVector.get(j); 
      String title = (String) hm.get("title"); 
      String describ = (String) hm.get("describ"); 

      Label z = new Label(title); 
      Label zz = new Label(describ); 
      f.add(z); 
      f.add(zz); 
     } 
    } catch (IOException ex) { 
    } 
} 

답변

1

은 TBH 내가 문제를 얻을 didnt는 구체적으로 말하지만 여전히 시도해 볼 수 있도록 몇 가지 샷을 제공하려고합니다.

제가 올바르게 이해한다면 20 개의 값을 가진 객체가 있습니까? 그렇다면 JSONArray를 가지고 그냥 물마루를 반복하고 JSONObject를 잡아라. 가되지 않습니다으로

지금 여기

나는 당신이 그것을 얻을 didnt는 생각 내 코드

JSONArray jsonTasks = new JSONArray(responseString); 
      for (int index = 0; index < jsonTasks.length(); index++) { 
          JSONObject jsonObject = (JSONObject) jsonTasks.get(index); 
          if (jsonObject != null) { 
           Map jsonMap = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(jsonObject.toString().getBytes(UTF8)), UTF8)); 
           System.out.println(jsonMap.get("date")); 
+0

의 짧은 조각입니다 ... 대신 구문 분석의 parseJSON를 사용합니다. json이 저장된 문자열 예 : 문자열 a = "[{json}]", 문자열 b = "[{other json}] 등은 객체의 값과 동일한 이름을 가짐 사용자가 값"a " , 나는 json 문자열 변수를 구문 분석 할 & 등등. 또한 하나의 여러 개체를 선택할 수 있고 json 동시에 구문 분석해야합니다. – beck

+0

두 질문 및이 주석을 읽었습니다 및 나는 당신이 무엇을 요구하는 모르겠다. .. –

관련 문제