Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将多个JSON对象提取到字符串中_Java_Android_Json_Android Json - Fatal编程技术网

Java 如何将多个JSON对象提取到字符串中

Java 如何将多个JSON对象提取到字符串中,java,android,json,android-json,Java,Android,Json,Android Json,我正在从文件中读取多个JSONObject,并使用StringBuilder将其转换为字符串 这些是JSON对象 {"Lng":"-1.5908601","Lat":"53.7987816"} {"Lng":"-2.5608601","Lat":"54.7987816"} {"Lng":"-3.5608601","Lat":"55.7987816"} {"Lng":"-4.5608601","Lat":"56.7987816"} {"Lng":"-5.560837","Lat":"57.7987

我正在从文件中读取多个
JSONObject
,并使用
StringBuilder
将其转换为字符串

这些是JSON对象

{"Lng":"-1.5908601","Lat":"53.7987816"}
{"Lng":"-2.5608601","Lat":"54.7987816"}
{"Lng":"-3.5608601","Lat":"55.7987816"}
{"Lng":"-4.5608601","Lat":"56.7987816"}
{"Lng":"-5.560837","Lat":"57.7987816"}
{"Lng":"-6.5608294","Lat":"58.7987772"}
{"Lng":"-7.5608506","Lat":"59.7987823"}
如何转换成字符串

实际代码为:-

BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;

        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        }
        catch(IOException e)
        {
            msg.Log(e.toString());
        }

        String contentsAsString = builder.toString();
        //msg.Log(contentsAsString);
我试过这个密码

JSONObject json = new JSONObject(contentsAsString);

Iterator<String> iter = json.keys();
  while(iter.hasNext())
    {
       String key = iter.next();
         try{
             Object value = json.get(key);
             msg.Log("Value :- "+ value);
         }catch(JSONException e)
          {
              //error
          }
    }
JSONObject json=新的JSONObject(contentsAsString);
迭代器iter=json.keys();
while(iter.hasNext())
{
String key=iter.next();
试一试{
Object value=json.get(key);
msg.Log(“值:-”+值);
}捕获(JSONException e)
{
//错误
}
}

它只是给出了第一个对象。如何循环它们?

试试这个,看看它是如何为您工作的

BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));

ArrayList<JSONObject> contentsAsJsonObjects = new ArrayList<JSONObject>();
while(true)
{
  String str = in.readLine();
  if(str==null)break;
  contentsAsJsonObjects.add(new JSONObject(str));
}

for(int i=0; i<contentsAsJsonObjects.size(); i++) 
{ 
  JSONObject json = contentsAsJsonObjects.get(i);
  String lat = json.getString("Lat"); 
  String lng = json.getString("Lng"); 
  Log.i("TAG", lat + lng) 
}
BufferedReader中的
=新的BufferedReader(新的文件读取器(“foo.in”);
ArrayList contentsAsJsonObjects=新的ArrayList();
while(true)
{
字符串str=in.readLine();
如果(str==null)中断;
添加(新的JSONObject(str));
}

对于(int i=0;i您要做的是将多个JSON对象加载到一个JSON对象中。这没有意义——只有第一个对象被解析是合乎逻辑的,解析器不希望在第一个
}
之后发生任何事情。因为您想要循环加载的对象,所以应该将它们加载到JSON数组中

如果可以编辑输入文件,请通过添加大括号和逗号将其转换为数组

[
    {},
    {}
]

如果不能,将括号追加到<代码> StringBuilder 的开头,并将逗号附加到每个加载的行。考虑附加条件,以消除由IpPuror输入文件引起的异常。

最后,您可以从字符串创建JSON数组,并使用此代码在其上循环

JSONArray array = new JSONArray(contentsAsString);

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

}
JSONArray数组=新的JSONArray(contentsastring);
对于(int i=0;i
什么是ContentsString?但您的stringbuilder中已经有了字符串格式的ContentsString,您到底想做什么。Joel-ContentsString是如上所示的对象列表,它是字符串格式。faljbour-我希望它们位于单独的字符串变量中,以便使用它们作为示例。
(int i=0;i@faljbour-sry我无法在评论中以适当的格式发表评论。