2014-11-03 51 views
0

我对Java的JSON解析相当陌生,但是当我尝试解析这个JSON字符串&发现它是“ID”时,它重复两次。Java - JSONObject只解析1个字符串?

[ 
    {"id":"{ID1}","time":123}, 
    {"id":"{ID2}","time":124} 
] 

这是我的Java代码:

 // v = json string, c = "id" 
     String output = v.replace("[", "").replace("]", ""); 
     JSONObject obj = new JSONObject(output); 
     ArrayList<String> list = new ArrayList<String>(); 

     for(int i = 0 ; i < obj.length(); i++){ 
      System.out.println(obj.getString(c)); 
      list.add(obj.getString(c)); 
     } 

     return list.get(1); 

返回ID1的两倍以上。请帮助

+2

json是一个数组,不要删除数组符号(即'['和']') – DwB 2014-11-03 17:35:23

+0

那么保持那里&保持原样? – ct12 2014-11-03 17:37:43

+0

在你的循环中,你使用循环索引“i”?如何知道要引用哪个数组元素? – 2014-11-03 17:40:25

回答

1

我通过使用它作为一个JSONArray(感谢@HotLicks)

JSONArray obj = new JSONArray(v); 
      ArrayList<String> list = new ArrayList<String>(); 

      for(int i = 0 ; i < obj.length(); i++){ 
       Logger.WriteOutput(obj.getJSONObject(i).getString(c), Logger.LogLevel.Info); 
      } 
-1

使用下面的代码

String v = "[{\"id\":\"ID1\",\"time\":123},{\"id\":\"ID2\",\"time\":124}]"; 
     String c = "id"; 
     JSONArray obj = null; 
     try { 
      obj = new JSONArray(v); 

      ArrayList<String> list = new ArrayList<String>(); 

      for (int i = 0; i < obj.length(); i++) { 
       JSONObject j = (JSONObject) obj.get(i); 
       System.out.println(j.getString(c)); 
       list.add(j.getString(c)); 
      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

注意,我稍微纠正json的结构太

以前 [ { “ID”: “{ID1}”, “时间” :123},{ “ID”: “{ID2}”, “时间”:

后124} ] [ { “ID”: “ID1”, “时间”:123}, { “ID”: “ID2”, “时间”:124} ]

+0

拥有''{ID1}“ '不**使JSON无效。 – 2014-11-03 18:47:39

+0

你是对的 - 如果他有目的地认为有“{ID1}”作为字符串。由于他对JSON相当陌生,他的文章谈到了刚刚返回ID1(不包含大括号),所以我修改了输入字符串。 – Aun 2014-11-03 23:47:23

0

首先为您的JSON创建一个Java豆(例如here):

public class Item { 

    @JsonProperty("id") 
    private String id; 
    @JsonProperty("time") 
    private Integer time; 

    public final String getId() { 
     return id; 
    } 

    public final void setId(String id) { 
     this.id = id; 
    } 

    public final Integer getTime() { 
     return time; 
    } 

    public final void setTime(Integer time) { 
     this.time = time; 
    } 

} 

如果您正在使用杰克逊的Java JSON处理器,您可以创建从JSON字符串列表是这样的:

ObjectMapper objectMapper = new ObjectMapper(); 

    try { 
     List<Item> items = objectMapper.readValue(
        yourJSONString, 
        objectMapper.getTypeFactory().constructCollectionType(List.class, Item.class)); 

     for (Item item : items) { 
      System.out.println(item.getId()); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+1

这里绝对不需要创建一个bean。 – 2014-11-03 17:49:02

+0

@JonSkeet味道问题 – dit 2014-11-03 17:53:23

+0

对于我来说,对于给定的任务而言,它看起来更加努力。 (在其他情况下,它当然是非常合适的 - 但是比较你为*不完整*程序提供的代码量和我的较短代码是相同输出的独立程序。) – 2014-11-03 17:55:10

1

你的JSON表示数组 - 所以这是怎么你应该解析它。然后,您可以轻松地从阵列中的每个JSONObject获取id属性。例如:

import org.json.*; 

public class Test { 
    public static void main(String[] args) throws JSONException { 
     String json = 
      "[{\"id\":\"{ID1}\",\"time\":123}, {\"id\":\"{ID2}\",\"time\":124}]"; 
     JSONArray array = new JSONArray(json); 

     for (int i = 0; i < array.length(); i++) { 
      JSONObject o = array.getJSONObject(i); 
      System.out.println(o.getString("id")); 
     } 
    } 
} 

输出:

{ID1} 
{ID2} 
+0

有人下来投票给我们,不知道为什么... – dit 2014-11-03 19:31:07

+0

@dit:确实...这些事情发生。不用担心... – 2014-11-03 19:33:13

1

尝试此固定我的代码:

// This line is useless 
// String output = v.replace("[", "").replace("]", ""); 
JSONArray arr = new JSONArray(output); 
ArrayList<String> list = new ArrayList<String>(); 
for(int i = 0 ; i < arr.length(); i++){ 
    System.out.println(arr.getJSONObject(i).getString(c)); 
    list.add(arr.getJSONObject(i).getString(c)); 
}