JSONArray名称值对修改
问题描述:
我有一个字符串遵循特定模式,我想将子字符串写入JSON数组对象。JSONArray名称值对修改
我有使用正则表达式的数组中的每个子字符串。
现在我想使用jsonarray对象将所有这些子字符串写入json文件中。
我已经能够做到这一点很好,但在JSON观众名称值对显示
0 abcde
1 pqrs
我要修改这些名称(即0和1)以特定的字符串。怎么做?
以下是我的代码。
String introduction="<p>abcde</p><p>pqrs</p><p>xyz</p>";
JSONArray intro_paragraphs = new JSONArray();
Matcher m = Pattern.compile(Pattern.quote("<p>")+ "(.*?)"+ Pattern.quote("</p>")).matcher(introduction);
while(m.find())
{
String match_intro = m.group(1);
intro_paragraphs.put(match_intro);
obj.put("Section_Detailed_Introduction", intro_paragraphs);
}
输出是:::
[]Section_Detailed_Introduction
0 abcde
1 pqrs
2 xys
我想:::
para_1 abcde
para_2 pqrs
para_3 xyz
答
作出的代码稍作修改就可以实现这个
String introduction="<p>abcde</p><p>pqrs</p><p>xyz</p>";
JSONObject intro_paragraphs = new JSONObject();
JSONObject obj=new JSONObject();
Matcher m = Pattern.compile(Pattern.quote("<p>")+ "(.*?)"+ Pattern.quote("</p>")).matcher(introduction);
int i=1;
String key="para_";
while(m.find())
{
String match_intro = m.group(1);
intro_paragraphs.put(key+i, match_intro);
i++;
}
obj.put("Section_Detailed_Introduction", intro_paragraphs);
+0
谢谢你..你的解决方案工作得很好.. –
代码你发布不会产生任何输出。产出如何?至于构建一个包含字符串的JSONArray,看起来很好。 'obj.put()'不需要在循环中;只做一次。 – dsh
好的...但修改名称 - 值对名称的任何提示? –
JSONArray不是名称 - 值对结构的类型。一个Map是或者一个JSONObject。 'obj'是一个Map还是其他的东西?你不显示它的声明。 – dsh