<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[json-c array Sortieren]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte, meine JSON Inhalt in einer array nach einem Bestimmten Wert Sortieren lassen,<br />
nutze die Bibliothek json-c und es gib unter:</p>
<p><a href="https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/sort.md" rel="nofollow">https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/sort.md</a></p>
<p>ein Beispiel, für eine einfache Sortierung, meine JSON sieht aber so aus:</p>
<pre><code>   [
       {
           &quot;POS&quot;: 0,
           &quot;ID&quot;: &quot;2545327648&quot;,
           &quot;UPDATE&quot;: 1755892547,
           &quot;FILE_SIZE&quot;: &quot;34428674&quot;,
           &quot;IS_ACTIV&quot;: &quot;false&quot;
       },
       {
           &quot;POS&quot;: 4,
           &quot;ID&quot;: &quot;1559212036&quot;,
           &quot;UPDATE&quot;: 1745501111,
           &quot;FILE_SIZE&quot;: &quot;525685&quot;,
           &quot;IS_ACTIV&quot;: &quot;false&quot;
       },
       {
           &quot;POS&quot;: 2,
           &quot;ID&quot;: &quot;2796703209&quot;,
           &quot;UPDATE&quot;: 1720644377,
           &quot;FILE_SIZE&quot;: &quot;1174863&quot;,
           &quot;IS_ACTIV&quot;: &quot;true&quot;
       },
       {
           &quot;POS&quot;: 1,
           &quot;ID&quot;: &quot;2291785308&quot;,
           &quot;UPDATE&quot;: 1756819745,
           &quot;FILE_SIZE&quot;: &quot;51587989&quot;,
           &quot;IS_ACTIV&quot;: &quot;false&quot;
       },
       {
           &quot;POS&quot;: 3,
           &quot;ID&quot;: &quot;2116151222&quot;,
           &quot;UPDATE&quot;: 1755866671,
           &quot;FILE_SIZE&quot;: &quot;122657596&quot;,
           &quot;IS_ACTIV&quot;: &quot;ture&quot;
       }
   ]
</code></pre>
<p>und möchte das ganze nach POS aufsteigend Sortieren lassen.</p>
<p>Sorry habe aber absolut keinen Ansatzplan, wie ich das umsetzte.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/355352/json-c-array-sortieren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 10 Oct 2026 17:38:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/355352.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 Sep 2025 05:49:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to json-c array Sortieren on Fri, 05 Sep 2025 05:49:00 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte, meine JSON Inhalt in einer array nach einem Bestimmten Wert Sortieren lassen,<br />
nutze die Bibliothek json-c und es gib unter:</p>
<p><a href="https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/sort.md" rel="nofollow">https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/sort.md</a></p>
<p>ein Beispiel, für eine einfache Sortierung, meine JSON sieht aber so aus:</p>
<pre><code>   [
       {
           &quot;POS&quot;: 0,
           &quot;ID&quot;: &quot;2545327648&quot;,
           &quot;UPDATE&quot;: 1755892547,
           &quot;FILE_SIZE&quot;: &quot;34428674&quot;,
           &quot;IS_ACTIV&quot;: &quot;false&quot;
       },
       {
           &quot;POS&quot;: 4,
           &quot;ID&quot;: &quot;1559212036&quot;,
           &quot;UPDATE&quot;: 1745501111,
           &quot;FILE_SIZE&quot;: &quot;525685&quot;,
           &quot;IS_ACTIV&quot;: &quot;false&quot;
       },
       {
           &quot;POS&quot;: 2,
           &quot;ID&quot;: &quot;2796703209&quot;,
           &quot;UPDATE&quot;: 1720644377,
           &quot;FILE_SIZE&quot;: &quot;1174863&quot;,
           &quot;IS_ACTIV&quot;: &quot;true&quot;
       },
       {
           &quot;POS&quot;: 1,
           &quot;ID&quot;: &quot;2291785308&quot;,
           &quot;UPDATE&quot;: 1756819745,
           &quot;FILE_SIZE&quot;: &quot;51587989&quot;,
           &quot;IS_ACTIV&quot;: &quot;false&quot;
       },
       {
           &quot;POS&quot;: 3,
           &quot;ID&quot;: &quot;2116151222&quot;,
           &quot;UPDATE&quot;: 1755866671,
           &quot;FILE_SIZE&quot;: &quot;122657596&quot;,
           &quot;IS_ACTIV&quot;: &quot;ture&quot;
       }
   ]
</code></pre>
<p>und möchte das ganze nach POS aufsteigend Sortieren lassen.</p>
<p>Sorry habe aber absolut keinen Ansatzplan, wie ich das umsetzte.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624171</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624171</guid><dc:creator><![CDATA[s0pply]]></dc:creator><pubDate>Fri, 05 Sep 2025 05:49:00 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Fri, 05 Sep 2025 06:32:38 GMT]]></title><description><![CDATA[<p>Dafür benötigst du dann die Funktion <a href="https://json-c.github.io/json-c/json-c-0.10/doc/html/json__object_8h.html#ac11730ad909d1f9eb077d1ce9ff8b153" rel="nofollow">json_object_object_get</a>, um ein Unterelement (z.B. <code>&quot;POS&quot;</code>) zu lesen.<br />
Rufe diese dann zuerst anstatt von <code>json_object_get_int</code> oder <code>json_object_get_string</code> (aus deinem gezeigten Link) auf und vergleiche dann dessen Werte (entsprechend des Datentyps).</p>
<p>PS: In deinem letzten Element steht <code>&quot;IS_ACTIV&quot;: &quot;ture&quot;</code> (anstatt <code>&quot;true&quot;</code>;- )<br />
Und im englischen heißt es <code>active</code> (mit <code>e</code>).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624172</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624172</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Fri, 05 Sep 2025 06:32:38 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Wed, 10 Sep 2025 18:43:02 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>vielen dank für die kleinen Hinweise der Schreibfehler, leider komme ich so nicht weiter, hier mal mein Code aber Verstehe nicht, wie ich dies Sortieren kann:</p>
<p>JSON:</p>
<pre><code>{
    &quot;LIST&quot;: [
		       {
		           &quot;POS&quot;: 0,
		           &quot;ID&quot;: &quot;2545327648&quot;,
		           &quot;UPDATE&quot;: 1755892547,
		           &quot;FILE_SIZE&quot;: &quot;34428674&quot;,
		           &quot;IS_ACTIVE&quot;: &quot;false&quot;
		       },
		       {
		           &quot;POS&quot;: 4,
		           &quot;ID&quot;: &quot;1559212036&quot;,
		           &quot;UPDATE&quot;: 1745501111,
		           &quot;FILE_SIZE&quot;: &quot;525685&quot;,
		           &quot;IS_ACTIVE&quot;: &quot;false&quot;
		       },
		       {
		           &quot;POS&quot;: 2,
		           &quot;ID&quot;: &quot;2796703209&quot;,
		           &quot;UPDATE&quot;: 1720644377,
		           &quot;FILE_SIZE&quot;: &quot;1174863&quot;,
		           &quot;IS_ACTIVE&quot;: &quot;ture&quot;
		       },
		       {
		           &quot;POS&quot;: 1,
		           &quot;ID&quot;: &quot;2291785308&quot;,
		           &quot;UPDATE&quot;: 1756819745,
		           &quot;FILE_SIZE&quot;: &quot;51587989&quot;,
		           &quot;IS_ACTIVE&quot;: &quot;false&quot;
		       },
		       {
		           &quot;POS&quot;: 3,
		           &quot;ID&quot;: &quot;2116151222&quot;,
		           &quot;UPDATE&quot;: 1755866671,
		           &quot;FILE_SIZE&quot;: &quot;122657596&quot;,
		           &quot;IS_ACTIVE&quot;: &quot;ture&quot;
		       }
		   ] 
}
</code></pre>
<p>C:</p>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;json-c/json.h&gt;

int main(int argc, char const *argv[]) {
	json_object *JSON_FILE = json_object_from_file(&quot;./Config.json&quot;);
	json_object *LIST = json_object_object_get(JSON_FILE, &quot;LIST&quot;);

	size_t LIST_Len = json_object_array_length(LIST);

	for (int a = 0; a &lt; LIST_Len; a++) {
            json_object *OBJ = json_object_array_get_idx(LIST, a);
            json_object *ID = json_object_object_get(OBJ, &quot;ID&quot;);
            json_object *POS = json_object_object_get(OBJ, &quot;POS&quot;);
            printf(&quot;\t[%d] = %s (%s)\n&quot;, (int)a + 1, json_object_get_string(ID), json_object_get_string(POS));
        }

	return 0;
}
</code></pre>
<p>Ausgabe:</p>
<pre><code>[1] = 2545327648 (0)
[2] = 1559212036 (4)
[3] = 2796703209 (2)
[4] = 2291785308 (1)
[5] = 2116151222 (3)
</code></pre>
<p>Ich weiß leider nicht wie ich dies jetzt nach POS Sortieren kann.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624175</guid><dc:creator><![CDATA[s0pply]]></dc:creator><pubDate>Wed, 10 Sep 2025 18:43:02 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Thu, 11 Sep 2025 07:15:20 GMT]]></title><description><![CDATA[<p>Du hast doch selber den Link auf das Sortier-Tutorial gepostet.<br />
Dort ist doch ein weiterer Link auf das Beispielprogramm <a href="https://github.com/json-c/json-c/blob/master/tests/test1.c" rel="nofollow">test1.c</a> (welches du auch mal einfach bei dir lokal ausführen kannst).</p>
<p>Und in der Sortier(vergleichs)funktion <code>sort_fn</code>, welche du dann für dein Programm übernimmst, änderst du entsprechend den Code (wie von mir oben beschrieben) ab. Und das Sortieren selbst wird mit der Funktion <code>json_object_array_sort(...)</code> ausgeführt (wie auch im Tutorial direkt als erstes beschrieben).</p>
<p>PS: Du hast jetzt immer noch den ersten Schreibfehler in deiner JSON-Datei drin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624176</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624176</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Thu, 11 Sep 2025 07:15:20 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Thu, 11 Sep 2025 14:46:25 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/18594">@Th69</a> sagte in <a href="/forum/post/2624176">json-c array Sortieren</a>:</p>
<blockquote>
<p>PS: Du hast jetzt immer noch den ersten Schreibfehler in deiner JSON-Datei drin.</p>
</blockquote>
<p>Hab jetzt mal mit 0 und 1 getauscht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Leider kann ich deine Beschreibung nicht ganz folgen, habe jetzt Versucht, dies so Umzusetzten:</p>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;json-c/json.h&gt;
#include &lt;string.h&gt;

static int sort_fn(const void *j1, const void *j2) {
	json_object *const *jso1, *const *jso2;
	int i1, i2;

	jso1 = (json_object * const *)j1;
	jso2 = (json_object * const *)j2;
	if (!*jso1 &amp;&amp; !*jso2)
		return 0;
	if (!*jso1)
		return -1;
	if (!*jso2)
		return 1;

	i1 = json_object_get_int(*jso1);
	i2 = json_object_get_int(*jso2);

	return i1 - i2;
}

int main(int argc, char const *argv[]) {

	json_object *JSON_FILE = json_object_from_file(&quot;./Config.json&quot;);
	json_object *LIST = json_object_object_get(JSON_FILE, &quot;LIST&quot;);

    json_object_array_sort(LIST, sort_fn);

    printf(&quot;my_array=\n&quot;);
    for (int i = 0; i &lt; json_object_array_length(LIST); i++)
    {
        json_object *obj = json_object_array_get_idx(LIST, i);
        printf(&quot;\t[%d]=%s\n&quot;, (int)i, json_object_to_json_string(obj));
    }

	return 0;
}
</code></pre>
<p>Ausgabe</p>
<pre><code>my_array=
        [0]={ &quot;POS&quot;: 4, &quot;ID&quot;: &quot;2545327648&quot;, &quot;UPDATE&quot;: 1755892547, &quot;FILE_SIZE&quot;: &quot;34428674&quot;, &quot;IS_ACTIVE&quot;: 1 }
        [1]={ &quot;POS&quot;: 0, &quot;ID&quot;: &quot;1559212036&quot;, &quot;UPDATE&quot;: 1745501111, &quot;FILE_SIZE&quot;: &quot;525685&quot;, &quot;IS_ACTIVE&quot;: 1 }
        [2]={ &quot;POS&quot;: 2, &quot;ID&quot;: &quot;2796703209&quot;, &quot;UPDATE&quot;: 1720644377, &quot;FILE_SIZE&quot;: &quot;1174863&quot;, &quot;IS_ACTIVE&quot;: 0 }
        [3]={ &quot;POS&quot;: 1, &quot;ID&quot;: &quot;2291785308&quot;, &quot;UPDATE&quot;: 1756819745, &quot;FILE_SIZE&quot;: &quot;51587989&quot;, &quot;IS_ACTIVE&quot;: 0 }
        [4]={ &quot;POS&quot;: 3, &quot;ID&quot;: &quot;2116151222&quot;, &quot;UPDATE&quot;: 1755866671, &quot;FILE_SIZE&quot;: &quot;122657596&quot;, &quot;IS_ACTIVE&quot;: 1 }
</code></pre>
<p>Nach Sortierung sieht das nun nicht wirklich aus, mein alter Kopf qualmt schon</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624177</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624177</guid><dc:creator><![CDATA[s0pply]]></dc:creator><pubDate>Thu, 11 Sep 2025 14:46:25 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Fri, 12 Sep 2025 05:59:00 GMT]]></title><description><![CDATA[<p>Und wo greifst du in der Sortierfunktion jetzt auf <code>&quot;POS&quot;</code> zu?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624178</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624178</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Fri, 12 Sep 2025 05:59:00 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Fri, 12 Sep 2025 08:47:58 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/38969">@s0pply</a> sagte in <a href="/forum/post/2624177">json-c array Sortieren</a>:</p>
<blockquote>
<p>i1 = json_object_get_int(*jso1);</p>
</blockquote>
<p>Wenn ich die Doku mir richtig angeschaut habe, musst du stattdessen:</p>
<ol>
<li><code>json_object_object_get_ex</code> aufrufen, um dir das &quot;POS&quot;-json_object zu holen</li>
<li>auf das Ergebnis davon dann <code>json_object_get_int</code> aufrufen.</li>
</ol>
<p>Also (absolut ungetestet):</p>
<pre><code>const json_object *pos_jso;
bool ok = json_object_object_get_ex(*jso1, &quot;POS&quot;, &amp;pos_jso);
if (!ok) handle_pos_missing();
int i1 = json_object_get_int(pos_jso);
// entweder hier testen, ob errno == EINVAL oder vorher feststellen, 
// ob das wirklich ein int ist - das get_int scheint nach der Doku auch
// automatisch doubles zu konvertieren - und es returnt 0, wenn da 
// was nicht geht - oder mit 0 glücklich werden ;-)

ok = json_object_object_get_ex(*jso2, &quot;POS&quot;, &amp;pos_jso);
if (!ok) handle_pos_missing();
int i2 = json_object_get_int(pos_jso);


</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2624179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624179</guid><dc:creator><![CDATA[wob]]></dc:creator><pubDate>Fri, 12 Sep 2025 08:47:58 GMT</pubDate></item><item><title><![CDATA[Reply to json-c array Sortieren on Fri, 12 Sep 2025 12:33:32 GMT]]></title><description><![CDATA[<p>Das war der entscheidende Tip aber musste:</p>
<pre><code class="language-c">const json_object *pos_jso;
</code></pre>
<p>auf:</p>
<pre><code class="language-c">struct json_object *pos_jso;
</code></pre>
<p>ändern, dass das geht, hatte gestern Abend mir eine Umständliche Lösung gebaut und kam darauf:</p>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;json-c/json.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

typedef struct {
	int sn_pos;
	char sn_id[12];
	int sn_update;
	char sn_file_size[12];
	int sn_active;
} sort_sn;

int sn_sort( const void *left, const void *right ) {
	const sort_sn *b = ( const sort_sn *)left;
	const sort_sn *a = ( const sort_sn *)right;

	if (b-&gt;sn_pos &lt; a-&gt;sn_pos) {
		return -1;
	} else if (a-&gt;sn_pos &lt; b-&gt;sn_pos) {
		return 1;
	} else {
		return (a-&gt;sn_id &lt; b-&gt;sn_id) - (b-&gt;sn_id &lt; a-&gt;sn_id);
	}
}

int main(int argc, char const *argv[]) {

	json_object *JSON_FILE = json_object_from_file(&quot;./Config.json&quot;);
	json_object *LIST = json_object_object_get(JSON_FILE, &quot;LIST&quot;);

	size_t LIST_Len = json_object_array_length(LIST);

	sort_sn meineDatenArray[LIST_Len];

	for (int a = 0; a &lt; LIST_Len; a++) {
		json_object *OBJ = json_object_array_get_idx(LIST, a);
		json_object *ID = json_object_object_get(OBJ, &quot;ID&quot;);
		json_object *POS = json_object_object_get(OBJ, &quot;POS&quot;);
		json_object *UPDATE = json_object_object_get(OBJ, &quot;UPDATE&quot;);
		json_object *FILE_SIZE = json_object_object_get(OBJ, &quot;FILE_SIZE&quot;);
		json_object *IS_ACTIVE = json_object_object_get(OBJ, &quot;IS_ACTIVE&quot;);

		meineDatenArray[a].sn_pos = json_object_get_int(POS);
		strcpy(meineDatenArray[a].sn_id, json_object_get_string(ID));
		meineDatenArray[a].sn_update = json_object_get_int(UPDATE);
		strcpy(meineDatenArray[a].sn_file_size, json_object_get_string(FILE_SIZE));
		meineDatenArray[a].sn_active = json_object_get_int(IS_ACTIVE);
	}

	qsort(meineDatenArray, LIST_Len, sizeof(sort_sn), sn_sort);

    for (size_t i = 0; i &lt; LIST_Len; i++) {
        printf( &quot;%d, %s, %s\n&quot;, meineDatenArray[i].sn_pos, meineDatenArray[i].sn_id, meineDatenArray[i].sn_title );
    }

	return 0;
}
</code></pre>
<p>Natürlich ist der Weg weit aus länger aber war mein Gedanke mal anders anzugehen.</p>
<p>Wie gesagt, Dank dir für die Hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2624180</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2624180</guid><dc:creator><![CDATA[s0pply]]></dc:creator><pubDate>Fri, 12 Sep 2025 12:33:32 GMT</pubDate></item></channel></rss>